经典搜索题目

hdu 1238

思路对了,题目被A的可能就大了

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<cctype>
#include<algorithm>
#include <vector>
#include <queue>

using namespace std;
#define INF 0x3f3f3f3f
#define N 200

char s[N][N], minstr[N], str[N], revstr[N], tmp[N];
int minlen, n;

int judge(char s1[], int k)
{
    strncpy(str, s1, k);//strncpy的用处,真的很好用,复制后一个字符串最多n个字符到第一个字符串
    strcpy(tmp, str);
    strrev(tmp);
    strcpy(revstr, tmp);

    for(int i=0; i<n; i++)
    {
        if(strstr(s[i], str)==0&&strstr(s[i], revstr)==0)//在前一个字符串中搜索看有没有第二个字符串,没有时返回NULL(也就是0)
            return 0;
    }
    return 1;
}

int solve()
{
    for(int i=minlen; i>=0; i--)
    {
        memset(str, 0, sizeof(str));
        memset(revstr, 0, sizeof(revstr));
        for(int j=0; j+i<=minlen; j++)
        {
            if(judge(minstr+j, i))
                return i;
        }
    }
    return 0;
}

int main()
{
    int T;
    scanf("%d", &T);

    while(T--)
    {
        scanf("%d", &n);
        minlen=INF;
        for(int i=0; i<n; i++)
        {
            scanf("%s", s[i]);
            if(strlen(s[i])<minlen)
            {
                minlen=strlen(s[i]);
                strcpy(minstr, s[i]);
            }
        }

        int ans=solve();

        printf("%d\n", ans);
    }
    return 0;
}

 

posted on 2016-05-25 22:23  南风丶丶  阅读(272)  评论(0编辑  收藏  举报