标签:strlen gets one put line sim careful main str
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16230 Accepted Submission(s): 5790
#include <cstdio> #include <iostream> #include <cstring> using namespace std; const int MAXN=26; //只有小写字母 typedef struct Trie{ bool v; Trie *next[MAXN]; }Trie; Trie *root; char s1[100]; char s2[100]; void createTrie(char *str) { int len=strlen(str); Trie *p=root,*q; for(int i=0;i<len;i++) { int id=str[i]-‘a‘; if(p->next[id]==NULL) { q=(Trie*)malloc(sizeof(Trie)); q->v=false; //每次建立新节点进行初始化操作 for(int j=0;j<MAXN;j++) q->next[j]=NULL; p->next[id]=q; p=p->next[id]; } else { // p->next[id]->v=false; p=p->next[id]; } } if(p!=root) //p==root 代表读入的是空串 p->v=true; //表示以此结点结尾的字符串存在 } bool findTrie(char *str) { int len=strlen(str); Trie *p=root; for(int i=0;i<len;i++) { int id=str[i]-‘a‘; p=p->next[id]; if(p==NULL) return false; } return p->v; } int deal(Trie *T) { int i; if(T==NULL) return 0; for(int i=0;i<MAXN;i++) { if(T->next[i]!=NULL) deal(T->next[i]); } free(T); return 0; } int main() { char str[50010][100]; int t,n,flag,cnt; root=(Trie*)malloc(sizeof(Trie)); for(int i=0;i<MAXN;i++) root->next[i]=NULL; root->v=false; //初始化 cnt=0; while(gets(str[cnt])) { createTrie(str[cnt]); cnt++; } // cout<<findTrie("a")<<" "<<findTrie("hat")<<endl; for(int k=0;k<cnt;k++){ n=strlen(str[k]); for(int i=1;i<=n;i++) { for(int j=0;j<i;j++) s1[j]=str[k][j]; s1[i]=‘\0‘; for(int j=i;j<=n-1;j++) s2[j-i]=str[k][j]; s2[n-i]=‘\0‘; if(findTrie(s1)&&findTrie(s2)) { printf("%s\n",str[k]); break; } } } deal(root); return 0; }
标签:strlen gets one put line sim careful main str
原文地址:http://www.cnblogs.com/a249189046/p/7468427.html