Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)
Total Submission(s): 21384 Accepted Submission(s): 9194
banana band bee absolute acm ba b band abc
2 3 1 0
#include<iostream> #include<map> #include<string> using namespace std; map<string,int >rec; int main() { string goal; char op; while(1) { op=getchar(); if(op=='\n') { op=getchar(); goal=""; } if(op=='\n')//连续两个 break; goal+=op; rec[goal]+=1; } while(cin>>goal) { cout<<rec[goal]<<endl; } return 0; }
#include <cstdio> #include <cstring> #define MAX 1000000+10 using namespace std; char str[1000]; char ask[1000]; int ch[MAX][40]; int word[MAX];//记录该节点下的单词数目 int val[MAX];//标记单词 结点 int sz; void init() { sz = 1; memset(ch[0], 0, sizeof(ch[0])); memset(word, 0, sizeof(word)); } int idx(char x) { return x - 'a'; } void insert(char *s) { int i, j, l = strlen(s); int u = 0; for(i = 0; i < l; i++) { int c = idx(s[i]); if(!ch[u][c]) { memset(ch[sz], 0, sizeof(ch[sz])); val[sz] = 0; ch[u][c] = sz++; } u = ch[u][c]; word[u]++; } val[sz] = 1;//标记单词节点 } int find(char *s) { int i, j, l = strlen(s); int u = 0; for(i = 0; i < l; i++) { int c = idx(s[i]); if(!ch[u][c]) return 0;//不存在 u = ch[u][c]; } return word[u]; } int main() { init(); while(gets(str), str[0]) { insert(str); } while(scanf("%s", ask) != EOF) { printf("%d\n", find(ask)); } return 0; }