Radio Transmission
KMP求n减next[n]得最短循环长度
正文
// 原题:https://oj.yecheng.tv/p/T1467
// 题意:给长度 L 和由某串自我连接而成的字符串,求生成它的最短串长度。
// 思路:求前缀函数 nxt,最长公共前后缀为 nxt[L],答案即最小循环节长度 L-nxt[L]。
// 复杂度:O(L) 时间 / O(L) 空间
// 易错点:直接输出 L-nxt[L] 即可,不需要整除判断,因为原串是循环串的子串而非整周期串。
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int L;
if(!(cin>>L)) return 0;
string s;
if(!(cin>>s)) return 0;
vector<int> nxt(L+1,0);
for(int i=1;i<L;i++){
int j=nxt[i];
while(j>0&&s[i]!=s[j]) j=nxt[j];
if(s[i]==s[j]) j++;
nxt[i+1]=j;
}
cout<<L-nxt[L]<<'\n';
return 0;
}
一本通·提高篇的其它内容
- 活动安排
- 种树
- 喷水装置
- 加工生产调度
- 智力大冲浪
- 数列极差
- 数列分段
- 线段
- 家庭作业
- 钓鱼
- 糖果传递
- 愤怒的牛
- Best Cow Fences
- 曲线
- 数列分段 II
- 扩散
- 灯泡
- 传送带
- 数的划分
- 生日蛋糕
- 小木棍
- Addition Chains
- 埃及分数
- 平板涂色
- 质数方阵
- 靶形数独
- 电路维修
- 魔板
- Knight Moves
- 棋盘游戏
- Keyboarding
- 移动玩具
- 山峰和山谷
- Oulipo
- 图书管理
- Power Strings
- Seekthe Name, Seek the Fame
- Friends
- A Horrible Poem
- Beads
- Antisymmetry
- 门票
- 收集雪花
- 剪花布条
- Power Strings
- OKR-Periods of Words
- 似乎在梦中见过的样子
- Censoring
- Phone List
- The XOR Largest Pair
- Nikitosh 和异或
- Immediate Decodability
- L 语言
- Secret Message 秘密信息
- 背单词
- The Xor-longest Path
- Keywords Search
- 玄武密码
- Censoring
- 单词