zoukankan      html  css  js  c++  java
  • 经典搜索题目

    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
    ", ans);
        }
        return 0;
    }
  • 相关阅读:
    Http协议(一)基础知识
    Redis问题汇总
    Sql Server存储过程传递XML参数
    C# Redis
    Task的异常捕获和处理方法
    Entity Framework教程
    WebBrowser 弹窗体关闭原理
    C# 虹软SDK视频人脸识别和注册
    C#性能优化:延迟初始化Lazy
    DZ 特殊主题简单开发教程
  • 原文地址:https://www.cnblogs.com/9968jie/p/5528769.html
Copyright © 2011-2022 走看看