标签:ase leaves contain 字典树 stream otto space cstring direct
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K (Java/Others)
Total Submission(s): 21658 Accepted Submission(s): 7228
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #include<queue> #include<map> #include<set> #include<vector> #include<cstdlib> #include<string> #define eps 0.000000001 typedef long long ll; typedef unsigned long long LL; using namespace std; const int N=100+10; struct tire{ char str[N]; int flag; struct tire *next[30]; }; tire *root; tire *init(){ tire *p=(tire *)malloc(sizeof(tire)); for(int i=0;i<30;i++){ p->next[i]=NULL; } // p->terminal=false; p->flag=0; return p; } void insert(char *a,char *b){ int i=0; tire *p=root; while(b[i]!=‘\0‘){ if(p->next[b[i]-‘a‘]==0){ p->next[b[i]-‘a‘]=init(); } p=p->next[b[i]-‘a‘]; //p->num++; i++; } p->flag=1; strcpy(p->str,a); } void find(char *str){ int i=0; tire *p=root; while(str[i]!=‘\0‘&&p->next[str[i]-‘a‘]){ p=p->next[str[i]-‘a‘]; i++; } if(str[i]==‘\0‘&&p->flag){ printf("%s",p->str); } else printf("%s",str); } int main() { char word[11],trans[11]; char s[20]; scanf("%s",word); root=init(); while(scanf("%s",word)!=EOF){ //输入字典 if(strcmp(word,"END")==0) //遇到结束标记 break; scanf("%s",s); insert(word,s); //将单词word插入到字典树中,并在最后加入其翻译 } scanf("%s",word); getchar(); char str[3001]; while(gets(str)){ if(strcmp(str,"END")==0) break; int i,j=0; char t[3001]={0}; for(i=0;str[i];i++){ if(‘a‘<=str[i] && str[i]<=‘z‘){ //检测到的是小写字母 t[j++] = str[i]; } else{ t[j] = ‘\0‘; if(t[0]!=‘\0‘){ //不是空的 find(t); //找到对应的翻译并输出,没找到则输出原来的字符串 t[0]=‘\0‘,j=0; //初始化t } printf("%c",str[i]); } } printf("\n"); } return 0; } /* int main(){ char str1[N],str2[N],str3[N],str4[N],s[N]; gets(str1); root=init(); while(scanf("%s",str2)!=EOF){ if(strcmp(str2,"END")==0)break; scanf("%s",str3); insert(str2,str3); } //getchar(); gets(str4); getchar(); char a[N]; //getchar(); while(gets(s)){ //find(s); int k=0; if(strcmp(s,"END")==0)break; int len=strlen(s); int flag=0; for(int i=0;i<len;i++){ if(s[i]>=‘a‘&&s[i]<=‘z‘){ a[k++]=s[i]; } else{ a[k]=‘\0‘; if(a[0]!=‘\0‘){ find(a); k=0; a[0]=‘\0‘; } printf("%c",s[i]); } } cout<<endl; } }*/
标签:ase leaves contain 字典树 stream otto space cstring direct
原文地址:http://www.cnblogs.com/Aa1039510121/p/6411212.html