codeforces 526D(kmp,数学)

description
One day Om Nom found a thread with n beads of different colors. He decided to cut the first several beads from this thread to make a bead necklace and present it to his girlfriend Om Nelly.

Om Nom knows that his girlfriend loves beautiful patterns. That’s why he wants the beads on the necklace to form a regular pattern. A sequence of beads S is regular if it can be represented as S = A + B + A + B + A + … + A + B + A, where A and B are some bead sequences, ” + ” is the concatenation of sequences, there are exactly 2k + 1 summands in this sum, among which there are k + 1 “A” summands and k “B” summands that follow in alternating order. Om Nelly knows that her friend is an eager mathematician, so she doesn’t mind if A or B is an empty sequence.

Help Om Nom determine in which ways he can cut off the first several beads from the found thread (at least one; probably, all) so that they form a regular pattern. When Om Nom cuts off the beads, he doesn’t change their order.

Input
The first line contains two integers n, k (1 ≤ n, k ≤ 1 000 000) — the number of beads on the thread that Om Nom found and number k from the definition of the regular sequence above.

The second line contains the sequence of n lowercase Latin letters that represent the colors of the beads. Each color corresponds to a single letter.

Output
Print a string consisting of n zeroes and ones. Position i (1 ≤ i ≤ n) must contain either number one if the first i beads on the thread form a regular sequence, or a zero otherwise.
Examples
input
7 2
bcabcab
output
0000011
input
21 2
ababaababaababaababaa
output
000110000111111000011
Note
In the first sample test a regular sequence is both a sequence of the first 6 beads (we can take A = “”, B = “bca”), and a sequence of the first 7 beads (we can take A = “b”, B = “ca”).

In the second sample test, for example, a sequence of the first 13 beads is regular, if we take A = “aba”, B = “ba”.


题目大意
给定字符串 s s 和一个数字k
对字符串每一个前缀求前缀 T T 能否由ABABAB......A这样的形式组成
其中 A A B都为字符串,且 A A k+1 B B 有k个
A B B 都能为空串


我们考虑,若一个字符串s能表示为 ABABABABA A B A B A B A B A
那么也可以表示为 CCCCA C C C C A A A C的一个前缀
那么这就成了一个类似于最小循环串的问题了
对于一个前缀 s s ,我们是否能找到一个循环串C,在原串中出现 k k 或正好k+1
我们考虑前缀 s s 的一个最小循环串T
s s 的任何循环串都可以表示为多个T接起来

假设 T T 的长度为a
那么我们的问题就转化成了已知 i,a,k i , a , k 找到一个正整数 t t
使得ita==k||i==at(k+1)
后面那个式子是很好处理的,问题就是前面那个式子怎么处理
推一下可以发现 ita=iat ⌊ i t ∗ a ⌋ = ⌊ ⌊ i a ⌋ t ⌋
那么问题就转化为了求解 xt==k ⌊ x t ⌋ == k
可以证得 t t 有整数解当且仅当 x/k>x% k k ,这也可以推得。

时间复杂度O(n)

#include<bits/stdc++.h>
using namespace std;
#define rep(i,j,k) for(int i = j;i <= k;++i)
#define repp(i,j,k) for(int i = j;i >= k;--i)
#define rept(i,x) for(int i = linkk[x];i;i = e[i].n)
#define P pair<int,int>
#define Pil pair<int,ll>
#define Pli pair<ll,int>
#define Pll pair<ll,ll>
#define pb push_back 
#define pc putchar
#define ll long long
int n , k;
int Next[1001000];
char s[1001000];
int read()
{
    int sum = 0;char c = getchar();bool flag = true;
    while( c < '0' || c > '9' ) {if(c == '-') flag = false;c = getchar();}
    while( c >= '0' && c <= '9' ) sum = sum * 10 + c - 48 , c = getchar();
    if(!flag)  sum = -sum;
    return sum;
} 
bool check(int a,int b,int k)
{
    int c = a/b;
    if(c/k > c%k) return true;
    if(a % (k + 1) == 0)
    {
        a /= (k+1);
        if(a % b == 0) return true;
    }   
    return false;
}
void init()
{
    n = read();k = read();
    scanf("%s",s+1);
    int j = 0;
    rep(i,2,n)
    {
        while(j && s[j+1] != s[i]) j = Next[j];
        if(s[i] == s[j+1]) j++;
        Next[i] = j;
    }
    rep(i,1,n)
    {
        int c = i - Next[i];
        if(check(i,c,k)) pc('1');
        else pc('0');
    }
}
int main()
{
    init();
    return 0;
}
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值