Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

HDU 2328 POJ 3450 KMP

$
0
0

题目链接:  HDU http://acm.hdu.edu.cn/showproblem.php?pid=2328

                  POJ http://poj.org/problem?id=3450


#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
using namespace std;
const int maxn=4444;

char x[222],ans[222]; 
char f[maxn][222];
int next[222];

void strcpy(char *y,int i,int len){
    for(int j=0;j<len;++i,++j)
        x[j]=y[i];
    x[len]='\0';
}
void Get_Next(char *s){
    int len=strlen(s);
    next[0]=-1;
    int i=0, j=-1;
    while(i<len){
        if(j==-1||s[i]==s[j]){
            if(s[++i]==s[++j])
                next[i]=next[j];
            else next[i]=j;
        }
        else j=next[j];
    }
}
bool KMP(char *s,char *a){//cout<<1<<endl;
    int ls=strlen(s), la=strlen(a);
    Get_Next(a);
    int i=0, j=0;
    while(i<ls&&j<la){
        if(j==-1||s[i]==a[j])
            ++i,++j;
        else j=next[j];
    }
    if(j>=la) return true;
    return false;
}
int main(){
    int n;
    while(~scanf("%d",&n),n){
        for(int i=0;i<n;++i)
            scanf("%s",f[i]);
        int len=strlen(f[0]);
        bool flag=false;
        ans[0]='\0';
        for(int i=len;i&&!flag;--i)
        for(int j=0,k;j<=len-i;++j){
            strcpy(f[0],j,i);
            for(k=1;k<n;++k)
                if(!KMP(f[k],x))break;
            if(k==n)//cout<<x<<endl;
                if(!flag||strcmp(ans,x)>0){
                    strcpy(ans,x); flag=true;
                }
        }
        if(flag)
            puts(ans);
        else 
            puts("IDENTITY LOST");
    }
    return 0;
}


作者:du489380262 发表于2013-7-17 13:37:10 原文链接
阅读:22 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>