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;
    }
  • 相关阅读:
    python中的编码问题
    CVPR2018 Tutorial 之 Visual Recognition and Beyond
    hdu 1376 Octal Fractions
    hdu 1329 Hanoi Tower Troubles Again!
    hdu 1309 Loansome Car Buyer
    hdu 1333 Smith Numbers
    hdu 1288 Hat's Tea
    hdu 1284 钱币兑换问题
    hdu 1275 两车追及或相遇问题
    hdu 1270 小希的数表
  • 原文地址:https://www.cnblogs.com/9968jie/p/5528769.html
Copyright © 2011-2022 走看看