题目大意

题目看样例也能猜到就是输出最短的循环串。

吐槽

明明是div3第一题为啥子还会用到kmp的知识?

解法

这个题仔细看发现是求最长可去除的后缀,也就是说去除跟下一个相同的字符串还能连接起来。这个不就是next数组的功能吗?最长公共前后缀。

公式:len-next[len]

我们把前k-1个字符串只输出前面的部分最后加上一个完整的字符串即可

完整代码

#include <bits/stdc++.h>
using namespace std;
char a[500];
int nex[500];
int maxn=-1;
void get()
{
int j=0;
for(int i=2;i<=strlen(a+1);i++)
{
while(j&&a[i]!=a[j+1])
j=nex[j];
if(a[i]==a[j+1])
j++;
nex[i]=j;
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,t;
cin>>n>>t;
cin>>a+1;
get();
int ans=strlen(a+1)-nex[strlen(a+1)];
for(int i=0;i<t-1;i++)
for(int j=1;j<=ans;j++)
cout<<a[j];
cout<<a+1;
}

Many Equal Substrings CodeForces - 1029A (kmp next数组应用)的更多相关文章

  1. HDU 1358 Period(KMP next数组运用)

    Period Problem Description For each prefix of a given string S with N characters (each character has ...

  2. CodeForces A. Many Equal Substrings

    http://codeforces.com/contest/1029/problem/A You are given a string tt consisting of nn lowercase La ...

  3. Codeforces 1137B(kmp next数组构造)

    为了物尽其用,Next求出最多有哪部分能重复使用,然后重复使用就行了-- const int maxn = 5e5 + 5; char s[maxn], t[maxn]; int cnts0, cnt ...

  4. Common Divisors CodeForces - 182D || kmp最小循环节

    Common Divisors CodeForces - 182D 思路:用kmp求next数组的方法求出两个字符串的最小循环节长度(http://blog.csdn.net/acraz/articl ...

  5. HDU 3333 | Codeforces 703D 树状数组、离散化

    HDU 3333:http://acm.hdu.edu.cn/showproblem.php?pid=3333 这两个题是类似的,都是离线处理查询,对每次查询的区间的右端点进行排序.这里我们需要离散化 ...

  6. POJ2406 Power Strings(KMP,后缀数组)

    这题可以用后缀数组,KMP方法做 后缀数组做法开始想不出来,看的题解,方法是枚举串长len的约数k,看lcp(suffix(0), suffix(k))的长度是否为n- k ,若为真则len / k即 ...

  7. POJ 2406 KMP/后缀数组

    题目链接:http://poj.org/problem?id=2406 题意:给定一个字符串,求由一个子串循环n次后可得到原串,输出n[即输出字符串的最大循环次数] 思路一:KMP求最小循环机,然后就 ...

  8. kmp next数组的理解(挺好的一篇文章 ,原来kmp最初的next是这样的啊,很好理解)

    KMP算法的next[]数组通俗解释   我们在一个母字符串中查找一个子字符串有很多方法.KMP是一种最常见的改进算法,它可以在匹配过程中失配的情况下,有效地多往后面跳几个字符,加快匹配速度. 当然我 ...

  9. POJ-3450 Corporate Identity (KMP+后缀数组)

    Description Beside other services, ACM helps companies to clearly state their “corporate identity”, ...

随机推荐

  1. H264--4--H264编码[7]

    ----------------------------------- 编码器输出格式 ---------------------------------- 总的来说H264的码流的打包方式有两种,一 ...

  2. 从service启动activity startActivity慢 的解决方案

    Intent intent = new Intent(context, A.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Pendin ...

  3. mybatis写当天 当月的数据 时间段数据

    1 数据库字段pk_time(Varchar) 当天的数据 SELECT * FROM 表 WHERE date(fk_time) = curdate(); 当月的数据 SELECT *FROM 表 ...

  4. ViewControl的size设为freeform

    freeform的用处是让你写一些不标准的view,比如说自定义一个cell,或者自己写一个小的VIEW,freeform的XIB是可以自己拖拽更改大小的

  5. [NOIP 2010] 引入入城

    [题目链接] https://loj.ac/problem/2595 [算法] 显然 , 每个第一行的成市控制的一定是一段区间 那么 , 问题就转化为了经典的区间覆盖问题 , 贪心即可 , 时间复杂度 ...

  6. AT2004 Anticube

    https://www.zybuluo.com/ysner/note/1304774 题面 给定\(n\)个数\(s_i\),要求从中选出最多的数,满足任意两个数之积都不是完全立方数. \(n\leq ...

  7. 51Nod 1967 路径定向 —— 欧拉回路

    题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1967 显然是欧拉回路问题,度数为奇数的点之间连边,跑欧拉回路就可以 ...

  8. bzoj1875 [SDOI2009]HH去散步——矩阵快速幂

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1875 有个限制是不能走回头路,比较麻烦: 所以把矩阵中的元素设成边的经过次数,单向边之间就好 ...

  9. jquery autocomplete自动补全

    简单用法: $(function(){ var data = "the People's Republic of China".split(" "); $(&q ...

  10. CMake使用总结

    总结CMake的常用命令,并介绍有用的CMake资源. CMake意为cross-platform make,可用于管理c/c++工程.CMake解析配置文件CMakeLists.txt生成Makef ...