HDU - 2328 Corporate Identity(kmp)

题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=2328

题意

同 poj3080 https://blog.csdn.net/a670531899/article/details/81570063

思路

直接暴力,不过(200^3)*4000居然都能过?好神奇的数据。

代码

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<cmath>
#include<map>
#include<algorithm>
#include<string>
#include<string.h>
#include<set>
#include<queue>
#include<stack>
#include<functional>
using std::pair;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<PII,int> PPI;
typedef pair<ll,int> PLI;
const double PI=acos(-1);
const int maxn = 2e2+10;
const int maxm = 1e6 + 10;
const int mod = 1e4+7;
const int INF = 0x3f3f3f3f;
using namespace std;
int nex[maxn];
void getNext(string t)
{
    nex[0]=-1;
    int j=0,k=-1;
    int len=t.length();
    while(j<len)
    {
        if(k==-1||t[j]==t[k]) nex[++j]=++k;
        else k=nex[k];
    }
}
int kmp(string s,string t)
{
    int i=0,j=0;
    int lens=s.length(),lent=t.length();
    while(i<lens&&j<lent)
    {
        if(j==-1||s[i]==t[j]) i++,j++;
        else j=nex[j];
        if(j==lent) return i-j+1;
    }
    return -1;
}
int main()
{
   string s[4010];
   int n;
   while(~scanf("%d",&n)&&n)
   {
       for(int i=0;i<n;i++) cin>>s[i];
       string t;
       string res;
       int ma=0;
       int len=s[0].length();
       for(int i=0;i<len;i++)
       {
           for(int j=i+1;j<=len;j++)
           {
               t=string(s[0].begin()+i,s[0].begin()+j);
               getNext(t);
               int f=0;
               for(int k=1;k<n;k++)
               {
                   if(kmp(s[k],t)==-1)
                   {
                       f=1;
                       break;
                   }
               }
               if(!f)
               {
                   if(ma<=j-i)
                   {
                       if(ma<j-i) res=t;
                       else res=min(res,t);
                       ma=j-i;
                   }
               }
           }
       }
       //cout<<ma<<endl;
       if(ma==0) printf("IDENTITY LOST\n");
        else
       cout<<res<<endl;
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值