标签:scan 难题 print 表单 tom c++ str while nbsp
#include <iostream> #include <cstring> using namespace std; char str[15]; struct TnT { TnT *next[26]; int v; TnT():v(0) { memset(next , 0 , sizeof(next)); } }; void build(TnT *root , char s[]) { TnT *p = root; int len = strlen(s); for(int i = 0 ; i < len ; i++) { int id = s[i] - ‘a‘; if(p->next[id] == NULL) { p->next[id] = new TnT; } p = p->next[id]; p->v++; } } void de(TnT *root) { if(root == NULL) return ; for(int i = 0 ; i < 26 ; i++) { de(root->next[i]); } delete root; } int find(TnT *root , char s[]) { int len = strlen(s); TnT *p = root; for(int i = 0 ; i < len ; i++) { int id = s[i] - ‘a‘; if(p->next[id] == NULL) { return 0; } p = p->next[id]; } return p->v; } int main() { TnT *p = new TnT; while(gets(str) && str[0]!=‘\0‘) build(p , str); while(scanf("%s", str) != EOF) { int ans = find(p , str); printf("%d\n", ans); } de(p); return 0; }
标签:scan 难题 print 表单 tom c++ str while nbsp
原文地址:http://www.cnblogs.com/TnT2333333/p/6052577.html