【bzoj3620】 似乎在梦中见过的样子 KMP
来源:互联网 发布:nosql与mysql的区别 编辑:程序博客网 时间:2024/04/29 11:59
一开始看到数据范围是15000就往后缀数组上想,想了好久没想出来怎么处理同一子串的问题,后来看了看题解才发现O(n^2)就可以过,用KMP类似NOI2014动物园的算法一样。枚举左端点,对于每个右端点处理出以右端点为结尾最大长度使得从左端点开始的前缀等于以右端点结束的后缀,即next数组。然后一直往前跳,直到长度小于子串长度的一半为止。
#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<iostream>#include<algorithm>#define maxn 20010using namespace std;int next[maxn];char s[maxn];int n,k,ans;void KMP(int p){for (int i=1;i<=n;i++) next[i]=p-1;for (int i=p+1;i<=n;i++){int j=next[i-1];while (j!=p-1 && s[j+1]!=s[i]) j=next[j];if (s[j+1]==s[i]) j++;next[i]=j;}int j=next[p];for (int i=p+1;i<=n;i++){while (j!=p-1 && s[j+1]!=s[i]) j=next[j];if (s[j+1]==s[i]) j++;while ((j-p+1)*2>=(i-p+1)) j=next[j];if (j-p+1>=k) ans++;}}int main(){scanf("%s",s+1);scanf("%d",&k);n=strlen(s+1);for (int i=1;i<=n;i++) KMP(i);printf("%d\n",ans);return 0;}
0 0
- 【BZOJ3620】似乎在梦中见过的样子 KMP
- 【bzoj3620】 似乎在梦中见过的样子 KMP
- 【bzoj3620】【似乎在梦中见过的样子】【kmp】
- 【BZOJ3620】似乎在梦中见过的样子【KMP】【暴力】
- BZOJ3620: 似乎在梦中见过的样子 KMP
- [BZOJ3620]似乎在梦中见过的样子(kmp)
- bzoj3620 似乎在梦中见过的样子 kmp
- BZOJ3620: 似乎在梦中见过的样子 解题报告
- 【bzoj3620】似乎在梦中见过的样子
- BZOJ3620 似乎在梦中见过的样子
- bzoj3620: 似乎在梦中见过的样子
- BZOJ_P3620 似乎在梦中见过的样子(KMP)
- 3620: 似乎在梦中见过的样子|Kmp+暴力
- 3620: 似乎在梦中见过的样子 KMP
- BZOJ 3620: 似乎在梦中见过的样子(kmp)
- BZOJ 3620: 似乎在梦中见过的样子 KMP
- BZOJ 3620 似乎在梦中见过的样子 KMP+暴力
- bzoj 3620: 似乎在梦中见过的样子 kmp暴力
- php关于url、文件、目录、ip的相关问题汇总
- C#泛型集合
- python3入门之赋值语句介绍
- 南邮 OJ 1054 回文回文!
- uva 10935 卡片游戏【队列】
- 【bzoj3620】 似乎在梦中见过的样子 KMP
- Difference between Properties and Attributes in JavaScript
- 设计模式之-----组合模式
- iOS contentOffset和contentInset的区别
- 关于Android的HAL的一些理解
- 【GDOI】模拟8.2总结
- 南邮 OJ 1055 叙拉古猜想
- Android分析主线程与子线程,以及子线程之间相互通信
- std::function与std::bind 函数指针