题意:给出一个字符串str,求出str中存在多少子串,使得这些子串既是str的前缀,又是str的后缀。从小到大依次输出这些子串的长度。

解法:利用KMP中next[ ]数组的性质,依次找到前缀、后缀匹配的字符串。

 1 #include<cstdio>
2 #include<cstdlib>
3 #include<cstring>
4 #include<iostream>
5 using namespace std;
6
7 const int N=400010;
8 int next[N],a[N];
9 int n;
10 char s[N];
11
12 void kmp()
13 {
14 memset(next,0,sizeof(next));
15 int p=0;
16 next[1]=0;
17 for (int i=2;i<=n;i++)
18 {
19 while (s[i]!=s[p+1] && p) p=next[p];
20 if (s[i]==s[p+1]) p++;
21 next[i]=p;
22 }
23 }
24 int main()
25 {
26 while (scanf("%s",s+1)!=EOF)
27 {
28 n=strlen(s+1);
29 kmp();
30 int t=0;
31 for (int i=n;i;i=next[i])
32 a[++t]=i;
33 for (int i=t;i>=1;i--)
34 printf("%d ",a[i]);
35 printf("\n");
36 }
37 return 0;
38 }

【poj 2752】Seek the Name, Seek the Fame(字符串--KMP)的更多相关文章

  1. Seek the Name, Seek the Fame POJ - 2752

    Seek the Name, Seek the Fame POJ - 2752 http://972169909-qq-com.iteye.com/blog/1071548 (kmp的next的简单应 ...

  2. (KMP)Seek the Name, Seek the Fame -- poj --2752

    http://poj.org/problem?id=2752 Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536 ...

  3. KMP POJ 2752 Seek the Name, Seek the Fame

    题目传送门 /* 题意:求出一个串的前缀与后缀相同的字串的长度 KMP:nex[]就有这样的性质,倒过来输出就行了 */ /************************************** ...

  4. POJ 2752 Seek the Name, Seek the Fame [kmp]

    Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17898   Ac ...

  5. poj 2752 Seek the Name, Seek the Fame(KMP需转换下思想)

    Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10204   Ac ...

  6. poj 2752 Seek the Name, Seek the Fame【KMP算法分析记录】【求前后缀相同的子串的长度】

    Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14106   Ac ...

  7. POJ 2752 Seek the Name, Seek the Fame(next数组运用)

    Seek the Name, Seek the Fame Time Limit: 2000MS        Memory Limit: 65536K Total Submissions: 24000 ...

  8. POJ 2752 Seek the Name,Seek the Fame(KMP,前缀与后缀相等)

    Seek the Name,Seek the Fame 过了个年,缓了这么多天终于开始刷题了,好颓废~(-.-)~ 我发现在家真的很难去学习,因为你还要陪父母,干活,做家务等等 但是还是不能浪费时间啊 ...

  9. poj 2752 Seek the Name, Seek the Fame (KMP纯模版)

    Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13840   Ac ...

  10. POJ 2752:Seek the Name, Seek the Fame

    Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13619 Accept ...

随机推荐

  1. 笔记:学习go语言的网络基础库,并尝试搭一个简易Web框架

    在日常的 web 开发中,后端人员常基于现有的 web 框架进行开发.但单纯会用框架总感觉不太踏实,所以有空的时候还是看看这些框架是怎么实现的会比较好,万一要排查问题也快一些. 最近在学习 go 语言 ...

  2. 跨域的几种方式以及call(),apply() bind()方法的作用和区别

    jsonp: jsonp 全称是JSON with Padding,是为了解决跨域请求资源而产生的解决方案,是一种依靠开发人员创造出的一种非官方跨域数据交互协议. 一个是描述信息的格式,一个是信息传递 ...

  3. 【C++】《C++ Primer 》第十一章

    第十一章 关联容器 关联容器和顺序容器的不同:关联容器中的元素时按照关键字来保存和访问的. 关联容器支持通过关键字来高效地查找和读取元素,基本的关联容器类型是 map和 set. 类型 map 和 m ...

  4. SpringBoot同时接收单个对象和List<object>参数

    最近做项目的有个需求,是把多个文件移动到另一个文件夹下,这需要把 新的文件夹id -- Long类型 多个文件的信息 -- List< Object > 类型 这两个参数传给后台,我的后台 ...

  5. xtrabackup迁移mysql5.7.32

    问题描述:利用外部xtrabackup工具来做迁移mysql数据库,或者恢复数据库 xtrabackup迁移mysql 1.环境 mysql源库 mysql目标迁移库 IP 192.168.163.3 ...

  6. 【Oracle】想查询相关的v$视图,但是提示表或视图不存在解决办法

    原因是使用的用户没有相关的查询权限导致 解决办法: grant select  any dictionary to 用户;    --这个权限比较大 这个权限是最低的要求,但是可以访问到v$相关视图 ...

  7. P1273 有线电视网(树形动规,分组背包)

    题目链接: https://www.luogu.org/problemnew/show/P1273 题目描述 某收费有线电视网计划转播一场重要的足球比赛.他们的转播网和用户终端构成一棵树状结构,这棵树 ...

  8. web dynpro配置注意事项

    如果你想使用web dynpro 开发的应用,但是发现浏览器报错,那么你按照下面的步骤逐一进行检查吧.特别是返回的500错误,或者是你发现浏览器的地址栏中以http://<hostname> ...

  9. 安装git-macOS系统

    通过homebrew安装Git 1.安装homebrew /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/H ...

  10. springboot项目启动并立即执行自定义程序内容

    第一种:实现ApplicationRunner接口,重写其中的run()方法: 第二种:实现CommandLineRunner接口,重写其中的run()方法: 还有第三种...