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; }
 

 

转载于:https://www.cnblogs.com/Tianwell/p/11214465.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值