zoukankan      html  css  js  c++  java
  • hdu1238--Substrings

    暴力求解

    题意:求一个公共子串的最大长度,反转的公共子串存在也算。

    求解思路:先找出最短的字符串进行暴力枚举。每截取一个子串后,求出它的反转字符串,然后检验这两个子字符串是否存在输入的字符串组中,每个字符串只要存在子字符串和的翻转串其中一个就行。

    #include<cstdio>
    #include<string>
    #include<iostream>
    using namespace std;
    #define max(a,b) a>b?a:b
    
    int n,len,id;
    string str[110];
    bool check(string sub)
    {
        string tmp;
        int sl=sub.size();
        for(int i=0;i<sl;i++)
            tmp+=sub[sl-1-i];
        for(int i=0;i<n;i++)
            if(str[i].find(sub)==str[i].npos && str[i].find(tmp)==str[i].npos)
                return false;
        return true;
    }
    void Deal()
    {
        int ans=0;
        string tmp;
        for(int i=0;i<str[id].size();i++)
            for(int j=str[id].size()-1;j>=i;j--)
            {
                if((j-i+1)<ans) continue;
                tmp=str[id].substr(i,j-i+1);
                if(check(tmp))
                    ans=max(ans,(j-i+1));
            }
        printf("%d
    ",ans);
    }
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            len=200;
            scanf("%d",&n);
            for(int i=0;i<n;i++)
            {
                cin>>str[i];
                if(str[i].size()<len)
                {
                    len=str[i].size();
                    id=i;
                }
            }
            Deal();
        }
    }
  • 相关阅读:
    BUG记录
    .Net HTTP请求的发送方式与分析
    初始token
    VS2017开发安卓应用(Xamarin)
    路由模板和路由特性
    使用signalR创建聊天室。
    C# SessionHelper
    postgres递归查询所有子部门
    centos7备份postgres
    Centos7挂载硬盘
  • 原文地址:https://www.cnblogs.com/yongren1zu/p/3238148.html
Copyright © 2011-2022 走看看