POJ 3080 Blue Jeans

来源:互联网 发布:motion graphic软件 编辑:程序博客网 时间:2024/05/11 05:00

题目链接:http://poj.org/problem?id=3080

参考:http://blog.csdn.net/u010489766/article/details/9272489

//对第一个的串的每个子串都进行kmp匹配算法#include<stdio.h>#include<string.h>char s[12][70],temp[70];int next[70];int m,lent,lens;void getnext(){int i = 0,j = -1;next[0] = -1;while(i <= lent){if(j == -1 || temp[i] == temp[j]){i++;j++;next[i] = j;}elsej = next[j];}}bool kmp(int k){int i = 0,j = 0;while(j < lens){if(i == -1 || s[k][j] == temp[i]){i++;j++;}elsei = next[i];if(i == lent)//如果存在,直接返回return 1;}return 0;}int main(){int T;scanf("%d",&T);while(T--){memset(s,0,sizeof(s));int ansi = 0,ansj = 0,maxlen = -1;char ans[70];memset(ans,0,sizeof(ans));scanf("%d",&m);for(int i = 1;i <= m; i ++)scanf("%s",s[i]);for(int i = 0 ; i <= 59 ; i++){for(int j = i+1 ;j <= 59 ; j ++)//枚举子串{int t,flag = 0,cnt = 0;memset(next,0,sizeof(next));memset(temp,0,sizeof(temp));for(int k = i ; k <= j; k ++)temp[cnt++] = s[1][k];//把子串赋给temp[]lent = j - i + 1;lens = 60;getnext();for(int k = 1; k <= m ; k ++){t = kmp(k);//看s[k]中是否存在该子串if(t == 0){flag = 1;break;}}if(flag == 0){if(lent >= 3){if(maxlen < lent||(maxlen == lent && strcmp(temp,ans) < 0))//要么长度长,要么字母序靠前,就是字母序小{ansi = i;ansj = j;maxlen = lent;for(int k = 0; k < cnt;k++)ans[k] = temp[k];}}}}}if(ansi == 0 && ansj == 0)printf("no significant commonalities\n");else{for(int i = ansi; i <= ansj ; i ++)printf("%c",s[1][i]);printf("\n");}}return 0;}