标签:style blog http io ar color os sp for
//125ms #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #include<algorithm> #include<stack> #include<queue> #include<cctype> #include<sstream> using namespace std; #define pii pair<int,int> #define LL long long int const int eps=1e-8; const int INF=1000000000; const int maxn=1; struct trie { trie *child[26]; int v; }; trie root; void create_trie(char *ss) { int len=strlen(ss); trie *p=&root,*q; for(int i=0;i<len;i++) { int id=ss[i]-‘a‘; if(p->child[id]==NULL) { q=(trie*)malloc(sizeof(trie)); q->v=1; for(int j=0;j<26;j++) { q->child[j]=NULL; } p->child[id]=q; p=p->child[id]; } else { p->child[id]->v++; p=p->child[id]; } } } int find_trie(char*ss) { int len=strlen(ss); trie *p=&root; for(int i=0;i<len;i++) { int id=ss[i]-‘a‘; if(p->child[id]==NULL) return 0; p=p->child[id]; } return p->v; } int main() { //freopen("in8.txt","r",stdin); char str[15]; while(gets(str)&&(str[0]!=‘\0‘)) { create_trie(str); } while(scanf("%s",str)!=EOF) { printf("%d\n",find_trie(str)); } return 0; }
//1171ms #include <iostream> #include <map> #include <cstring> #include <string> using namespace std; int main() { int i, len; char str[10]; map<string, int> m; while( gets(str) ) { len = strlen(str); if ( !len ) { break; } for(i = len; i > 0; i--) { str[i] = ‘\0‘; m[str]++; } } while( gets(str) ) { cout << m[str] << endl; } return 0; }
标签:style blog http io ar color os sp for
原文地址:http://www.cnblogs.com/zywscq/p/4127602.html