Codeforces 526D Om Nom and Necklace kmp+hash
来源:互联网 发布:淘宝怎么赚钱 编辑:程序博客网 时间:2024/06/15 18:07
题目链接:点击打开链接
题意:
给出长度为n的字符串,常数k
下面一个长度为n的字符串。
问:
for(int i = 1; i <= n; i++){
字符串的前i个字符 能否构成 形如A+B+A+B+A+B+A的形式,其中A有k+1个,B有k个 A和B是2个任意的字符串(也可以为空串)
若可以构成则输出1,否则输出0
}
思路:
POJ1961
先用kmp求一个前缀循环节,。
我们观察 ABABABA => AB, AB, AB, A 所以前缀循环节有K个,而后面的A是尽可能地和AB长度接近,所以hash+二分求A的最长长度。
思路2:
直接枚举AB串的长度,然后二分A串的长度即可。
#include <iostream>#include <cstdio>#include <algorithm>#include <string>#include <cmath>#include <cstring>#include <queue>#include <set>#include <map>#include <vector>template <class T>inline bool rd(T &ret) {char c; int sgn;if (c = getchar(), c == EOF) return 0;while (c != '-' && (c<'0' || c>'9')) c = getchar();sgn = (c == '-') ? -1 : 1;ret = (c == '-') ? 0 : (c - '0');while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');ret *= sgn;return 1;}template <class T>inline void pt(T x) {if (x <0) {putchar('-');x = -x;}if (x>9) pt(x / 10);putchar(x % 10 + '0');}using namespace std;const int N = 1000005;typedef long long ll;typedef unsigned long long ull;const int maxn = 2000 * 1000 + 100, base1 = 131, base2 = 127;const int mod = 1000000007;int hash1[maxn], hash2[maxn], p1[maxn], p2[maxn];inline int get(int l, int r){l--;int ret = hash1[r] - 1LL * hash1[l] * p1[r - l ] % mod;if (ret<0)ret += mod;return ret;}int n, k, dp[N];char s[N];void work(){memset(dp, 0, sizeof dp);for (int y = n / k; y; y--){int len = y*k;if (dp[len] != 0)continue;ull now = get(1, y);bool ok = true;for (int j = 2; j <= k && ok; j++){if (get(j*y - y + 1, j*y) != now)ok = false;}if (ok)dp[len]++;else {continue;}int las = len;int l = len + 1, r = min(len + y, n);while (l <= r){int mid = (l + r) >> 1;if (get(1, mid - len) == get(len + 1, mid)){las = max(las, mid);l = mid + 1;}else r = mid - 1;}dp[las + 1] --;}}int hehe;int haha;int main(){p1[0] = 1;for (int i = 1; i<maxn; i++)p1[i] = 1LL * p1[i - 1] * base1%mod;scanf("%d%d", &n, &k);scanf("%s", s + 1);for (int i = 1; i <= n; i++)hash1[i] = (1LL * hash1[i - 1] * base1 + s[i]) % mod;if (k > n){ while (n-- > 0)putchar('0'); puts(""); return 0; }work();int now = 0;for (int i = 1; i <= n; i++){now += dp[i];putchar('0' + (now>0));}puts("");return 0;}
0 0
- Codeforces 526D Om Nom and Necklace kmp+hash
- Codeforces 526D Om Nom and Necklace 循环节 kmp
- CF 526D Om Nom and Necklace
- 判断s各个前缀是否是 ABABA…ABA的形式 KMP Codeforces 526D - Om Nom and Necklace
- D D - Om Nom and Necklace
- codeforces 526 d Om Nom and Necklace next数组的灵活运用
- Codeforces 526C Om Nom and Candies
- Codeforces 526C - Om Nom and Candies
- Codeforces 526C - Om Nom and Candies (贪心 + 思维)
- codeforces 526B Om Nom and Dark Park
- codeforces 526 c Om Nom and Candies 剩余系
- Codeforces 526C - Om Nom and Candies(贪心,暴力)
- CodeForces 526B Om Nom and Dark Park dp & dfs
- CodeForces 526B Om Nom and Dark Park
- Codeforces 526C Om Nom and Candies 完全背包
- 526B - Om Nom and Dark Park
- 526C - Om Nom and Candies
- CF 526C Om Nom and Candies
- self
- Oracle主键自增的解决办法
- VS的OpenCV配置助记
- static
- 使用Hibernate多对一关联表出现的小问题
- Codeforces 526D Om Nom and Necklace kmp+hash
- Android SDK Manger 无法更新问题解决方法 如果有其他更快的网址可以告诉我
- zoj 3640 Help Me Escape(概率DP基础)
- socket中的close和shutdown区别
- Python 编程学习 chapter2 数值与运算
- 日拱一卒(三十三)
- 【贪心专题】POJ 3258 River Hopscotch (最大化最小值 贪心+二分搜索)
- c&c++反汇编与逆向分析学习笔记(3)--基本数据类型表现形式
- 第三节 Informatica 体系架构之 Service Manager 服务管理器