zoukankan      html  css  js  c++  java
  • POJ-3080 Blue Jeans (暴力kmp)

    题意:找出所给的多个串的相同子串部分

    思路:暴力遍历(截取) 其中一个串的部分,然后将其用kmp简化与其他串的匹配 ,用ans来存储最终的答案,如果答案有多个则用 min()来取最小字典序

    完整代码:

    
    
    #include <cstdio>
    #include <cstring>
    #include <string>
    #include <iostream> 
    #include <algorithm>
    using namespace std;
    const int N=10+5;
    const int M=60+5;
    
    string s[N];
    int nex[M];
    
    void getnext(string str,int len){
        int i=0,j=-1;
        nex[0]=-1;
        while(i<len){
            if(j==-1||str[i]==str[j])
                nex[++i]=++j;
            else j=nex[j];
        }
    }
    bool kmp(string s1,int len1,string s2,int len2){
        int i=0,j=0;
        while(i<len1){
            if(j==-1||s1[i]==s2[j])
                ++i,++j;
            else j=nex[j];
            if(j==len2)return true;
        }
        return false;
    }
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);   
        int T,n;
        cin>>T;
        while(T--){
            cin>>n;
            for(int i=0;i<n;i++)cin>>s[i];
            string ans="";
            for(int i=0;i<s[0].size();i++) //起点
            {
                for(int j=1;i+j<=s[0].size();j++) //长度
                {
                    string res=s[0].substr(i,j);
                    getnext(res,res.size());
                    bool flag = false;
                    for(int k=1;k<n;k++)
                        if(!kmp(s[k],s[k].size(),res,res.size()))
                            flag= true;
                    if(!flag)
                    {
                        if(ans.size()<res.size()) ans=res;  //优先长度更长的公共子串
                        else if(ans.size()==res.size()) ans=min(ans,res); //长度相同,按字典序排序
                    }
                }
            }
            if(ans.size()<3)cout<<"no significant commonalities"<<endl;
            else cout<<ans<<endl;
        }
        return 0;
    }
     
  • 相关阅读:
    丑数系列
    452. 用最少数量的箭引爆气球
    406. 根据身高重建队列
    763. 划分字母区间
    所有二叉树题目记录
    二叉树前中后序遍历非递归(迭代)解法
    二叉树的层序遍历题目汇总
    442. 数组中重复的数据&&448. 找到所有数组中消失的数字
    225. 用队列实现栈(Easy)
    使用ClosedXML读写excel
  • 原文地址:https://www.cnblogs.com/Tianwell/p/11214465.html
Copyright © 2011-2022 走看看