标签:
字典树,题目并无难度,关键是如何输入空格推出循环
#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);
}
int main()
{
cin.sync_with_stdio(false);
while(getline(cin,str)&&str!="") insert(p,0);
while(cin>>str) solve(p,0);
return 0;
} 版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/zafkiel_nightmare/article/details/47703579