码迷,mamicode.com
首页 > 其他好文 > 详细

连续子序列的模板 stl

时间:2016-04-24 18:35:32      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

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] = \0;
                //printf("%s %s\n",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

 

连续子序列的模板 stl

标签:

原文地址:http://www.cnblogs.com/EdsonLin/p/5427554.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!