zoukankan      html  css  js  c++  java
  • Codevs 1425 最长公共子串

    1425 最长公共子串

     

     时间限制: 1 s
     空间限制: 128000 KB
     题目等级 : 青铜 Bronze
     
     
     
    题目描述 Description

    输入N(2<=N<=20)个字符串,输出最长公共子串。

    输入描述 Input Description

    输入N

    再输入N个字符串

    输出描述 Output Description

    输出最大公共子串。

    样例输入 Sample Input

    3

    abce

    cabk

    jaab

    样例输出 Sample Output

    ab

    /*
        好像这种求多串最长公共子串要用后缀数组或者自动机什么的
        我这个就比较暴力了
    */
    #include<iostream>
    #include<string>
    using namespace std;
    const int N=22;
    int INF=1<<30;
    int a[N],n1;
    string str[N];
    int main(){
        cin>>n1;
        for(int i=0;i<n1;i++){
            cin>>str[i];
            if(INF>str[i].size())
                INF=str[i].size();
        }
        if(n1==1){
            cout<<str[0];
            return 0;
        }
        for(int i=INF;i;i--){//枚举长度为INF的子串,从大往小 
            for(int j=0;j<str[0].size()-i;j++){//从第一个位置开始枚举子串 
                bool flag=true;
                for(int k=1;k<n1;k++)
                    if(str[k].find(str[0].substr(j,i) )== string::npos)
                       flag=false;
                       if(flag){
                           cout<<str[0].substr(j,i);
                           return 0;
                       }
            }
        } 
        return 0;
    }
  • 相关阅读:
    Storm 中drpc调用
    yarn下资源配置
    java 中 Stringbuff append源代码浅析
    总结的MR中连接操作
    hive中使用rcfile
    MapFile
    HDFS副本存放读取
    zoj 1967 Fiber Network/poj 2570
    zoj 2027 Travelling Fee
    poj 1742 Coins
  • 原文地址:https://www.cnblogs.com/thmyl/p/7290506.html
Copyright © 2011-2022 走看看