经典搜索题目

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;
}

 

转载于:https://www.cnblogs.com/9968jie/p/5528769.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值