poj 1226 暴力字符串匹配

来源:互联网 发布:开发长沙软件外包 编辑:程序博客网 时间:2024/06/10 05:33

题意:给定n个串,求一个最大子串长度,使得它或者它的逆向串在每个串中出现。

思路:先找个最短的串,然后枚举这个串的字串,并求出求逆串,然后在给定的n个串中暴搜即可。用库函数strstr即可。

#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>using namespace std;#define clr(s,t) memset(s,t,sizeof(s));#define INF 0x3fffffffint T,n,len;char s[105][105],t[105],w[105],q[105];int test(){    int i,j,k;    for(i = len;i>0;i--){        w[i] = '\0';        q[i] = '\0';        for(j = 0;j<=len-i;j++){            for(k = j;k<j+i;k++){                w[k-j] = t[k];                q[i-k+j-1] = t[k];            }            for(k = 1;k<=n;k++)                if(!strstr(s[k], w) && !strstr(s[k],q))                    break;            if(k == n+1)                return i;        }    }    return 0;}int main(){    scanf("%d",&T);    while(T--){        int i;        len = INF;        scanf("%d",&n);        for(i = 1;i<=n;i++) {            scanf("%s",s[i]);            if(strlen(s[i]) < len){                len = (int)strlen(s[i]);                strcpy(t, s[i]);            }        }        printf("%d\n",test());    }    return 0;}


0 0