Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
弱菜级别,代码仅供弱菜参考!#include <iostream> #include <string> #include <cstdio> using namespace std; int n,lem,mAx,next[210],len[5000]; string str[5000],sh,result; void getnext(); int init() { cin>>n;getchar(); if(n==0) return 0; for(int i=0;i<n;i++) { cin>>str[i]; len[i]=str[i].size(); } return 1; } void kmp() { getnext(); int i,j,k,ant; for(i=1;i<n;i++) { j=k=0;ant=-1; while(j<len[i]) { if(k==-1||sh[k]==str[i][j]) { k++;j++; } else k=next[k]; if(k>ant) ant=k; } if(ant<mAx) mAx=ant; } } int go() { int i,ant=-1; string temp_sh; for(i=0;i<len[0];i++) { sh=str[0].substr(i,len[0]-i); mAx=210; lem=sh.size(); //cout<<len[0]<<' '<<sh<<" "<<lem<<endl; kmp(); if(ant<mAx&&mAx!=0) { ant=mAx; result=sh.substr(0,ant); //cout<<result<<endl; } else if(ant==mAx) { temp_sh=sh.substr(0,ant); if(temp_sh<result) result=temp_sh; } } return ant; } int main () { while(init()) { int ant=go(); if(ant==-1) cout<<"IDENTITY LOST"<<endl; else cout<<result<<endl; } return 0; } void getnext() { int i=0,j; j=next[0]=-1; while(i<lem) { if(j==-1||sh[i]==sh[j]) { if(sh[j+1]==sh[i+1]) next[i+1]=next[j+1]; else next[i+1]=j+1; i++;j++; } else j=next[j]; } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator