【kmp算法next数组求解公共前后缀】Seek the Name, Seek the Fame POJ
来源:互联网 发布:中国期货数据 编辑:程序博客网 时间:2024/06/04 18:35
Think:
1知识点:通过kmp算法next数组求解公共前后缀
2题意:输入串s,输出所有的符合条件的子串的长度,符合条件:子串既是s的前缀又是s的后缀
3解题知识点:
(1):前缀定义:从s的开始字符到s的任意字符为止
(2):后缀定义:从s的任意字符到s的最后字符为止
(3):next[i]定义:字符s[i]及其前面至多有next[i]个连续的字符和字符串s从初始位置开始的next[i]个字符匹配
4解题思路:
求解next数组,然后递归输出路径,注意len_P需要单独输出
vjudge题目链接
建议参考博客——参考题意理解
以下为Accepted代码
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int size_P = 400414;int _next[size_P];char st[size_P];void get_next(char *P, int len_P);void pri_path(int n);int main(){ while(~scanf("%s", st)){ int len_P = strlen(st); get_next(st, len_P); pri_path(_next[len_P-1]); printf("%d\n", len_P); } return 0;}void get_next(char *P, int len_P){ int q, k; _next[0] = 0; k = 0; for(q = 1; q < len_P; q++){ while(k > 0 && P[q] != P[k]){ k = _next[k-1]; } if(P[q] == P[k]) k++; _next[q] = k; } return;}void pri_path(int n){ if(n == 0) return; pri_path(_next[n-1]); printf("%d ", n); return;}
阅读全文
0 0
- 【kmp算法next数组求解公共前后缀】Seek the Name, Seek the Fame POJ
- POJ 2752 Seek the Name, Seek the Fame KMP Next[]
- Seek the Name, Seek the Fame (POJ_2752) KMP + 最大公共前后缀
- POJ 2752 Seek the Name, Seek the Fame [前后缀相同的KMP]
- KMP + 求相等前后缀--- POJ Seek the Name, Seek the Fame
- poj 2752 Seek the Name, Seek the Fame KMP算法
- POJ 2752 Seek the Name, Seek the Fame kmp算法
- [KMP-NEXT数组性质]POJ 2752 Seek the Name, Seek the Fame
- POJ 2752 Seek the Name, Seek the Fame(KMP next数组应用)
- POJ 2752 Seek the Name, Seek the Fame(KMP的next数组)
- POJ 2752 Seek the Name, Seek the Fame(KMP,next数组的应用)
- POJ 2752 Seek the Name, Seek the Fame(KMP next数组运用)
- Seek the Name, Seek the Fame POJ 2752【KMP next数组的应用】
- POJ - 2752 Seek the Name, Seek the Fame(KMP next数组的理解)
- POJ 2752 Seek the Name, Seek the Fame (kmp next数组运用)
- POJ-2752 Seek the Name, Seek the Fame(kmp中next数组的应用)
- POJ 2752: Seek the Name, Seek the Fame(简单KMP-NEXT数组的应用)
- POJ 2752 Seek the Name, Seek the Fame(KMP+next数组的运用)
- 44.笔记go语言圣经——go平台与架构
- C++构造函数和析构函数中抛出异常
- 硬币组合问题之最少硬币个数
- 洛谷1419 寻找段落
- finally用法
- 【kmp算法next数组求解公共前后缀】Seek the Name, Seek the Fame POJ
- L3-001. 凑零钱
- 【雪球素数】worldline 题解
- PAT 甲级 1096. Consecutive Factors (20)
- LeetCode 63
- centos7网络环境配置(optenstack环境配置)
- 移动端布局单位
- 光增益中的参数详解:响应率,量子效率
- 数组中出现次数超过一半的数字