题目大意

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

吐槽

明明是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. robot framework运行测试 命令行启动

    ...\rf_test> pybot --test test_case test_suit.robot #运行一条用例 ...\rf_test> pybot test_suit.robot ...

  2. go10---struct

    package main import ( "fmt" ) type test struct{} //空的结构体 type person struct { name string ...

  3. 安装linux系统-CentOS-6.8-x86_64-minimal.iso

    1: 2: 3:单击[Next]继续安装. 4:安装语言,选择[Chinese(Simplified)(中文(简体))]菜单,单击[Next]继续. 5:系统键盘,选择[美国英语式]菜单,单击[下一步 ...

  4. rabbitmq最大连接数(Socket Descriptors)

    RabbitMQ自带了显示能够接受的最大连接数,有2种比较直观的方式:1. rabbitmqctl命令. 1 2 3 4 5 6 7 8 9 10 11 12 <span style=" ...

  5. preparing launch delegate问题

    启动tomcat成功,但是右下角一直显示100%点进去 发现preparing launch delegate卡在100% 以为是代理的事,把代理关了,还是不行,最后发现是数据库没有,导入数据库后完美 ...

  6. Python---scikit-learn(sklearn)模块

    Python在机器学习方面一个非常强力的模块---scikit-learn模块,它作为数据挖掘和数据分析方面的一个简单而有效的工具,主要包括6大功能:分类(Classification),回归(Reg ...

  7. EasyUI Form表单提交

    转自:https://www.cnblogs.com/net5x/articles/4576926.html Form(表单) 使用$.fn.form.defaults重写默认值对象 form提供了各 ...

  8. SVN请求认证信息

    WIN10:C:\Users\Home目录\AppData\Roaming\Subversion Window Server 系统:C:\Users\Home目录\AppData\Roaming\Su ...

  9. HDU 4691 后缀数组+RMQ

    思路: 求一发后缀数组,求个LCP 就好了 注意数字有可能不只一位 (样例2) //By SiriusRen #include <bits/stdc++.h> using namespac ...

  10. .net环境下程序一些未知错误的调试

    由于线程冲突等一系列原因导致的处理调试方法 1.打开[事件查看器]查找出错误的地方 [控制面板]-[系统和安全]-[管理工具]-[事件查看器]