hdu 1251 统计难题

来源:互联网 发布:中国电信测速软件下载 编辑:程序博客网 时间:2024/05/17 09:05

字典树,题目并无难度,关键是如何输入空格推出循环

用getline输入C++的字符串要加上 #include<string>..........
这题G++交会无限超内存
#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;} 



0 0