Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 16059 | Accepted: 6929 |
Description
Input
Output
Sample Input
carbohydrate cart carburetor caramel caribou carbonic cartilage carbon carriage carton car carbonate
Sample Output
carbohydrate carboh cart cart carburetor carbu caramel cara caribou cari carbonic carboni cartilage carti carbon carbon carriage carr carton carto car car carbonate carbona 题意:一堆字符串,求每个串的辨识前缀 思路:模拟了切割字符串,枚举切割点,查看前缀出现的次数,如果出现了一次,则说明这个前缀是辨识前缀,直接 输出,如果查找到最后仍没有查出来,就输出原串 ac代码:#include<stdio.h> #include<string.h> #include<math.h> #include<stack> #include<iostream> #include<algorithm> #define fab(a) (a)>0?(a):(-a) #define LL long long #define MAXN 55000 #define mem(x) memset(x,0,sizeof(x)) #define INF 0xfffffff using namespace std; struct s { int num; s *next[26]; }; s *root; char ss[MAXN][255]; void create(char *str) { int len=strlen(str); s *p=root,*q; for(int i=0;i<len;i++) { int id=str[i]-'a'; if(p->next[id]==NULL) { q=(s *)malloc(sizeof(s)); q->num=1; for(int j=0;j<26;j++) q->next[j]=NULL; p->next[id]=q; p=p->next[id]; } else { p->next[id]->num++; p=p->next[id]; } } //p->num=1; } int find(char *ss) { int len=strlen(ss); s *p=root; for(int i=0;i<len;i++) { int id=ss[i]-'a'; p=p->next[id]; if(p==NULL) return 0; } return p->num; } void begin() { for(int i=0;i<26;i++) root->next[i]=NULL; // root->num=0; } void freetree(s *t) { if(t==NULL) return; for(int i=0;i<26;i++) { if(t->next[i]!=NULL) freetree(t->next[i]); } free(t); return; } int main() { root=(s *)malloc(sizeof(s)); begin(); int k=0; char ne1[260],ne2[260]; while(scanf("%s",ss[k])!=EOF) { create(ss[k]); k++; } for(int i=0;i<k;i++) { int len=strlen(ss[i]); int bz=0; for(int j=1;j<len;j++) { int qq=0; for(int q=0;q<j;q++) ne1[qq++]=ss[i][q]; ne1[qq]='\0'; if(find(ne1)==1) { bz=1; printf("%s %s\n",ss[i],ne1); break; } } if(bz==0) printf("%s %s\n",ss[i],ss[i]); } return 0; }