D D - Om Nom and Necklace
来源:互联网 发布:湖南省地质测绘院 知乎 编辑:程序博客网 时间:2024/06/09 19:32
官方题解(讲的非常详细)
This task is to determine whether a string is in the form of ABABA... ABA for each prefixes of a given string S
For a prefix P, let's split it into some blocks, just like P = SSSS... SSSST, which T is a prefix of S. Obviously, if we use KMP algorithm, we can do it in linear time, and the length ofS will be minimal. There are only two cases :T = S, T ≠ S.
- T = S. When T = S, P = SSS... S. Assume thatS appears R times. Consider "ABABAB....ABABA", the lastA must be a suffix of P, and it must be like SS... S, so A will be like SS... SS, and so will B. By greedy algorithm, the length ofA will be minimal, so it will be SSS... S, where S appears times. AndB will be SSS...S, where S appears times. So we just need to check whether.
- T ≠ S . When T ≠ S, the strategy is similar to T = S. A will be like "SS...ST", and its length will be minimal. At last we just need to check whether .
The total time complexity is O(n).
AC代码
#include <queue>#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>using namespace std;const int MAX = 1000010;int n, k;char s[MAX];int next[MAX];void make_next(){int i = 0, j = -1;next[0] = -1;while(i < n){if(j == -1 || s[i] == s[j]){i++, j++;next[i] = j;}else j = next[j];}}int main(){scanf("%d%d", &n, &k);scanf("%s", s);make_next();for(int i=1; i<=n; i++){int cur = i - next[i];int L = i % cur;int cnt = i / cur;if(L == 0){if(cnt / k - cnt % k >= 0) printf("1");else printf("0");}else{if(cnt / k - cnt % k > 0) printf("1");else printf("0");}}puts("");}
0 0
- D D - Om Nom and Necklace
- CF 526D Om Nom and Necklace
- Codeforces 526D Om Nom and Necklace kmp+hash
- Codeforces 526D Om Nom and Necklace 循环节 kmp
- codeforces 526 d Om Nom and Necklace next数组的灵活运用
- 判断s各个前缀是否是 ABABA…ABA的形式 KMP Codeforces 526D - Om Nom and Necklace
- Om Nom and Dark Park
- Om Nom and Dark Park
- CF Om Nom and Candies
- Codefoces 436 B. Om Nom and Spiders
- 526B - Om Nom and Dark Park
- 526C - Om Nom and Candies
- CF 526C Om Nom and Candies
- B. Om Nom and Dark Park
- CF Om Nom and Dark Park
- Codeforces 526C Om Nom and Candies
- Codeforces 526C - Om Nom and Candies
- vj Om Nom and Dark Park
- nmon以及nmon analyser 简易教程
- 串口线与并口线的区别
- IOS Application生命周期
- !=小知识点
- 用B-树实现虚拟图书管理系统
- D D - Om Nom and Necklace
- PostgreSQL入门学习之数据库连接相关基础知识
- 【线段树】【4-6组队赛】Problem H
- 社説 20150409 日銀異次元緩和 「物価2%」の達成を焦るな
- Magento 用代码批量 添加产品的评论review(通过SKU)
- Windows和Linux下获取当前可执行文件路径和工作目录
- 人民日报:不告密不揭发是道德底线
- OPENGL 绘制基本图元
- tbb基础之parallel_reduce用法详解