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 |
可耻的贴代码 标准kmp#include <cstdio> #include <memory.h> using namespace std; char trademarks[4001][205]; int len[4001]; int mnext[4001]; void getsubstr(char *zhu,char *cong,int begin,int len) { memset(cong,0,sizeof(cong)); for(int i=0;i<=len-1;i++) { cong[i]=zhu[begin+i]; } cong[len]='\0'; } void getNext(char* s,int length) { memset(mnext,0,sizeof(mnext)); int i=0; int j=-1; mnext[0]=-1; while(i<=length-1) { if(j==-1||s[i]==s[j]) { i++; j++; mnext[i]=j; } else j=mnext[j]; } } bool kmpMatch(char *text,char *pattern,int tlen,int plen) { int i=0; int j=0; while(i<=tlen-1) { if(j==-1||text[i]==pattern[j]) { i++; j++; } else j=mnext[j]; if(j==plen) return true; } return false; } bool issmaller(char *t,char *p,int length) { for(int i=0;i<=length-1;i++) { if(t[i]<p[i]) return true; else if(t[i]>p[i]) return false; } return false; } int main() { int strnum; scanf("%d",&strnum); while(strnum!=0) { memset(trademarks,0,sizeof(trademarks)); memset(len,0,sizeof(len)); for(int i=1;i<=strnum;i++) { scanf("%s",trademarks[i]); int curlen=0; for(;trademarks[i][curlen]!='\0';curlen++) ; len[i]=curlen; } char result[205]; bool hasfound; memset(result,0,sizeof(result)); //找到一个子串,其length从最长到1,起始位置从0开始 for(int i=len[1];i>=1;i--) { hasfound=false; for(int j=0;j+i<=len[1];j++) { char temp[205]; getsubstr(trademarks[1],temp,j,i); getNext(temp,i); bool flag=true;//flag为true表示该子串是其他所有的子串 for(int k=2;k<=strnum;k++) { if(kmpMatch(trademarks[k],temp,len[k],i)==false) { flag=false; break; } } if(flag==true) { hasfound=true; if(result[0]=='\0'||issmaller(temp,result,i)==true) memcpy(result,temp,sizeof(result)); } } if(hasfound==true) break; } if(hasfound==true) printf("%s\n",result); else printf("IDENTITY LOST\n"); scanf("%d",&strnum); } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator