Topic Link: Click to open the link
Test instructions
Give a string of length n, constant k
The following is a string of length n.
Ask:
for (int i = 1; I <= n; i++) {
Whether the first I character of a string can form a shape such as A+b+a+b+a+b+a, where A has k+1, B has k A and B is 2 arbitrary string (also can be empty string)
Output 1 If it can be formed, otherwise output 0
}
Ideas:
POJ1961
First use KMP to find a prefix loop section.
We observed that the Abababa-AB, AB, AB, A had a previous-prefixed cycle section with K, while the back of a was as close as possible to the AB length, so hash+ the longest length of a.
Idea 2:
Directly enumerate the length of the AB string, and then the length of the two-string-a can be.
#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 * + (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);p Utchar (x% 10 + ' 0 ');} using namespace Std;const int N = 1000005;typedef long long ll;typedef unsigned long long ull;const int MAXN = 2000 * 1000 +, Base1 = 131, base2 = 127;const int mod = 1000000007;int HASH1[MAXN], HASH2[MAXN], P1[MAXN], P2[maxn];inline int ge T (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", &A Mp;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];p Utchar (' 0 ' + (now>0));} Puts (""); return 0;}
Codeforces 526D Om Nom and Necklace Kmp+hash