nowcoder 五 C

链接:https://www.nowcoder.com/acm/contest/77/C
来源:牛客网

题目描述

有一个字符串 让你找到这个字符串 S 里面的子串T 这个子串 T 必须满足即使这个串的前缀 也是这个
串的后缀 并且 在字符串中也出现过一次的(提示 要求满足前后缀的同时也要在字符串中出现一次 只是前后缀可不行 输出最长满足要求字符串)

输入描述:

给出一个字符串 长度 1 到 1e6  全部是小写字母

输出描述:

如果找的到就输出这个子串T 如果不行就输出 Just a legend

#include<bits/stdc++.h>
using namespace std;
#define maxn 1000000
string str;
int nex[maxn];
int s=0;
void get_nex(){              //获得next 数组
   nex[0]=-1;
   int k=-1,j=0;
   while(j<s){
      if(k==-1||str[j]==str[k]){
          j++;
          k++;
          nex[j]=k;
      }
      else k=nex[k];
   }
}
bool kmp(int len){                                  //kmp字符串匹配
    for(int i = 1, j = 0; i < s-1; i++){
        while(j > 0 && str[i] != str[j]) j = nex[j];
        if(str[i] == str[j]) j++;
        if(j == len) return true;
    }
    return false;
}
bool xxx(int len){                        //获取长度
    for(int j=0;j<len;j++){
       if(str[j]!=str[s-len+j]) return false;
    }
    return true;
}
bool work(){
   for(int j=s-2;j>=1;j--){
      //if(j==3) cout<<xxx(j)<<endl;
      if(xxx(j)){
         if(kmp(j)){
            for(int k=0;k<j;k++){
               cout<<str[k];
            }
            return true;
         }
      }
   }
   return false;
}
int main(){
   cin>>str;
   s=str.size();
   get_nex();
   if(!work()){
      cout<<"Just a legend"<<endl;
   }
   return 0;
}






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值