思路:

kmp略作修改。

实现:

  1. #include <iostream>
  2. #include <cstdio>
  3. using namespace std;
  4. const int MAXN = ;
  5. int neXt[MAXN];
  6.  
  7. void getNext(string s)
  8. {
  9. int n = s.length();
  10. neXt[] = -;
  11. int k = -, j = ;
  12. while (j < n)
  13. {
  14. if (k == - || s[j] == s[k])
  15. {
  16. j++; k++;
  17. neXt[j] = k;
  18. }
  19. else
  20. {
  21. k = neXt[k];
  22. }
  23. }
  24. }
  25.  
  26. bool kmp(string s, string p)
  27. {
  28. int i = , j = ;
  29. int m = s.length(), n = p.length();
  30. while (i < m && j < n)
  31. {
  32. if (j == - || s[i] == p[j]) i++, j++;
  33. else j = neXt[j];
  34. }
  35. if (j == n) return true;
  36. return false;
  37. }
  38.  
  39. int main()
  40. {
  41. string str;
  42. cin >> str;
  43. str += ' ';
  44. int n = str.length();
  45. getNext(str);
  46. int tmp = neXt[n - ];
  47. bool flg = false;
  48. while (tmp)
  49. {
  50. if (kmp(str.substr(, n - ), str.substr(, tmp)))
  51. {
  52. cout << str.substr(, tmp) << endl;
  53. flg = true;
  54. break;
  55. }
  56. tmp = neXt[tmp];
  57. }
  58. if (!flg) cout << "Just a legend" << endl;
  59. return ;
  60. }

CF126B Password的更多相关文章

  1. KMP CF126B Password

    Description Asterix,Obelix和他们的临时伙伴Suffix.Prefix已经最终找到了和谐寺.然而和谐寺大门紧闭,就连Obelix的运气也没好到能打开它. 不久他们发现了一个字符 ...

  2. CF126B password&&HDU 4763 Theme Section

    http://acm.hdu.edu.cn/showproblem.php?pid=4763 http://codeforces.com/problemset/problem/126/B 这两个题都是 ...

  3. CF126B Password【KMP】By cellur925

    题目传送门 其实$Chemist$在之前写了非常棒的题解! 我长话短说,补充两句. “那么当$next[n]$>$max$时显然不能将$next[n]$作为最长子串的长度”这句话其实在说,因为一 ...

  4. CF126B

    CF126B Password 题意: 给出一个字符串 H,找一个最长的字符串 h,使得它既作为前缀出现过.又作为后缀出现过.还作为中间的子串出现过. 解法: 沿着 $ next_n $ 枚举字符串, ...

  5. 浅谈KMP算法——Chemist

    很久以前就学过KMP,不过一直没有深入理解只是背代码,今天总结一下KMP算法来加深印象. 一.KMP算法介绍 KMP解决的问题:给你两个字符串A和B(|A|=n,|B|=m,n>m),询问一个字 ...

  6. 2021.08.30 前缀函数和KMP

    2021.08.30 前缀函数和KMP KMP算法详解-彻底清楚了(转载+部分原创) - sofu6 - 博客园 (cnblogs.com) KMP算法next数组的一种理解思路 - 挠到头秃 - 博 ...

  7. 「CF126B」Password

    题目描述 给定一个字符串 \(S\),我们规定一个字符串 \(P\) 是可行解,\(P\) 需要满足: \(P\) 是 \(S\) 的前缀 \(P\) 是 \(S\) 的后缀 \(P\) 出现在 \( ...

  8. 打开程序总是会提示“Enter password to unlock your login keyring” ,如何成功关掉?

    p { margin-bottom: 0.1in; line-height: 120% } 一.一开始我是按照网友所说的 : rm -f ~/.gnome2/keyrings/login.keyrin ...

  9. your password has expired.to log in you must change it

    今天应用挂了,log提示密码过期.客户端连接不上. 打开mysql,执行sql语句提示密码过期 执行set password=new password('123456'); 提示成功,但客户端仍然连接 ...

随机推荐

  1. RabbitMQ-rabbitMq各个特性的使用(三)

    准备 1.引入客户端和配置文件依赖类 <dependency> <groupId>com.rabbitmq</groupId> <artifactId> ...

  2. 最小生成树 B - Networking

    You are assigned to design network connections between certain points in a wide area. You are given ...

  3. 四则运算结对编程(GUI)

    四则运算GUI coding地址:https://git.dev.tencent.com/qyj814/GUI.git 结对伙伴:李梦宇 一.题目要求 定制出题要求.每次出题时用户都可以在界面上定制如 ...

  4. Ubuntu 16.04下没有“用户和组”功能的问题解决

    在16.04以前的版本会自带“用户和组”的功能,但是在16.04发现系统只自带了“用户账户”的功能. 问题解决: 1.安装gnome-system-tools sudo apt-get install ...

  5. react实现ssr服务器端渲染总结和案例(实例)

    1.什么是 SSR SSR 是 server side render 的缩写,从字面上就可以理解 在服务器端渲染,那渲染什么呢,很显然渲染现在框架中的前后端分离所创建的虚拟 DOM 2.为什么要实现服 ...

  6. Spring注解配置定时任务<task:annotation-driven/>

    http://m.blog.csdn.net/article/details?id=50945311 首先在配置文件头部的必须要有: xmlns:task="http://www.sprin ...

  7. 字符串的切割操作(strtok,split)

    一:strtok C/C++:char *strtok(char s[], const char *delim); s 代表须要切割的字符串,delim代表切割的标志,參数都为比选!返回指向切割部分的 ...

  8. ROBODK仿真如何设置运动速度

    设置工具-选项-运动,把仿真时间设置成跟正常一样   然后双击机器人,设置参数(可以设置movej和movel的速度,加速度)  

  9. HttpClient的Post和Get訪问网页

    一.基础JAR包 Mavenproject下pom.xml需配置的jar包 <dependencies> <dependency> <groupId>junit&l ...

  10. 配置hadoop集群一

    花了1天时间最终把环境搭建好了.整理了一下,希望对想学习hadoop的有所帮助. 资料下载:http://pan.baidu.com/s/1kTupgkn 包括了linux虚拟机.jdk, hadoo ...