• 连续子序列的模板 stl


    hdu1238 暴力搜

    Substrings

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 9122    Accepted Submission(s): 4302


    Problem Description
    You are given a number of case-sensitive strings of alphabetic characters, find the largest string X, such that either X, or its inverse can be found as a substring of any of the given strings.
     
    Input
    The first line of the input file contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case contains a single integer n (1 <= n <= 100), the number of given strings, followed by n lines, each representing one string of minimum length 1 and maximum length 100. There is no extra white space before and after a string. 
     
    Output
    There should be one line per test case containing the length of the largest string found.
     
    Sample Input
    2
    3
    ABCD
    BCDFF
    BRCD
    2
    rose
    orchid
     
    Sample Output
    2
    2
     
    思路:要求最长公共连续字串的,必然要从原来的最短的串那个入手,先找出那个序列,暴力分析所有的情况,一一匹配最大的情况,匹配连续序列时,可以用stl
     
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <cmath>
    #include <stack>
    #include <queue>
    #include <string>
    #include <algorithm>
    
    const int inf = (1<<31)-1;
    const int MAXN = 1e2+10;
    
    using namespace std;
    
    string s[MAXN];
    
    int main()
    {
        int t,n,ti,mmin;
        string ts1,ts2;
        scanf("%d",&t);
        while(t--){
            scanf("%d",&n);
            mmin = inf;
            for(int i=0;i<n;i++){
                //scanf("%s",s[i]);
                cin>>s[i];
                if(s[i].length()<mmin){
                    mmin = s[i].length();
                    ti = i;
                }
            }
            int mmax = 0,k;
          /*  for(int i=mmin;i>0;i--){
                for(int j=0;j<mmin-i+1;j++){
                    ts1 = s[ti].substr(j,i); //startpos lenth
                    ts2 = ts1;
                    reverse(ts2.begin(),ts2.end());
                   // cout<<ts1<<" "<<ts2<<endl;
                    for(k=0;k<n;k++){
                        if(s[k].find(ts1,0)==-1&&s[k].find(ts2,0)==-1)
                            break;
                    }
    
                    if(k==n&&mmax<ts1.length())
                        mmax = ts1.length();
                }
            }*/
            char ms1[MAXN],ms2[MAXN];
            for(int i=0;i<mmin;i++){
                for(int j=i;j<mmin;j++){
                    int b = 0;
                    for(int w=i;w<=j;w++){
                        ms1[b] = s[ti][w];
                        ms2[b] = s[ti][j-b];
                        b++;
                    }
                    //cout<<ms1<<" "<<ms2<<endl;
                    ms1[b] = ms2[b] = '';
                    //printf("%s %s
    ",ms1,ms2);
                    for(k=0;k<n;k++){
                        if(s[k].find(ms1,0)==-1&&s[k].find(ms2,0)==-1){
                            break;
    
                        }
                    }
                    if(k==n&&mmax<b)
                        mmax = b;
                }
            }
            cout<<mmax<<endl;
        }
        //cout << "Hello world!" << endl;
        return 0;
    }
    /*
    3
    123
    23333
    12222
    */
    View Code
    在一个谎言的国度,沉默就是英雄
  • 相关阅读:
    org.hibernate.QueryException: could not resolve property
    Eclipse支持文件UTF-8编码
    Eclipse External Tool Configration Notepad++
    WIN10 devtoolsuser
    Linux查看和注销用户(User)
    java list 去重
    监控系统对比 Ganglia vs Open-falcon vs Prometheus vs Zabbix vs Nagios vs PandoraFMS
    swagger 指定字段不显示到文档里
    mysql uuid() 相同 重复
    正则校验:微信号,qq号,邮箱
  • 原文地址:https://www.cnblogs.com/EdsonLin/p/5427554.html
Copyright © 2020-2023  润新知