HDU 1251 Statistical Puzzles

Dictionary tree. The problem is not difficult, the key is how to enter a space to push out the loop

To input C++ strings with getline, add #include<string>.........
This question G++ rendezvous with infinite super memory
#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
string str;
struct stu
{
	int m;
	stu * a [26];
	stu()
	{
		m=0;
		for(int i=0;i<26;i++) a[i]=NULL;
	}
};
stu * p = new stu ();
void insert(stu *root,int cnt)
{
	if(cnt==str.size()) return;
	int x=str[cnt]-'a';
	if(root->a[x]==NULL)
	{
		root->a[x]=new stu();
	}
	root=root->a[x];
	root->m++;
	insert(root,cnt+1);
}
void solve(stu *root,int cnt)
{
	int x=str[cnt]-'a';
	root=root->a[x];
	if(root==NULL)
	{
		cout<<"0"<<endl;
		return;
	}
	if(cnt==str.size()-1)
	{
		cout<<root->m<<endl;
	}
	else solve(root,cnt+1);
}
intmain()
{
	cin.sync_with_stdio(false);
	while(getline(cin,str)&&str!="") insert(p,0);
	while(cin>>str) solve(p,0);
	return 0;
}



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324653674&siteId=291194637