Many Equal Substrings CodeForces - 1029A (kmp next数组应用)
题目大意
题目看样例也能猜到就是输出最短的循环串。
吐槽
明明是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数组应用)的更多相关文章
- HDU 1358 Period(KMP next数组运用)
Period Problem Description For each prefix of a given string S with N characters (each character has ...
- CodeForces A. Many Equal Substrings
http://codeforces.com/contest/1029/problem/A You are given a string tt consisting of nn lowercase La ...
- Codeforces 1137B(kmp next数组构造)
为了物尽其用,Next求出最多有哪部分能重复使用,然后重复使用就行了-- const int maxn = 5e5 + 5; char s[maxn], t[maxn]; int cnts0, cnt ...
- Common Divisors CodeForces - 182D || kmp最小循环节
Common Divisors CodeForces - 182D 思路:用kmp求next数组的方法求出两个字符串的最小循环节长度(http://blog.csdn.net/acraz/articl ...
- HDU 3333 | Codeforces 703D 树状数组、离散化
HDU 3333:http://acm.hdu.edu.cn/showproblem.php?pid=3333 这两个题是类似的,都是离线处理查询,对每次查询的区间的右端点进行排序.这里我们需要离散化 ...
- POJ2406 Power Strings(KMP,后缀数组)
这题可以用后缀数组,KMP方法做 后缀数组做法开始想不出来,看的题解,方法是枚举串长len的约数k,看lcp(suffix(0), suffix(k))的长度是否为n- k ,若为真则len / k即 ...
- POJ 2406 KMP/后缀数组
题目链接:http://poj.org/problem?id=2406 题意:给定一个字符串,求由一个子串循环n次后可得到原串,输出n[即输出字符串的最大循环次数] 思路一:KMP求最小循环机,然后就 ...
- kmp next数组的理解(挺好的一篇文章 ,原来kmp最初的next是这样的啊,很好理解)
KMP算法的next[]数组通俗解释 我们在一个母字符串中查找一个子字符串有很多方法.KMP是一种最常见的改进算法,它可以在匹配过程中失配的情况下,有效地多往后面跳几个字符,加快匹配速度. 当然我 ...
- POJ-3450 Corporate Identity (KMP+后缀数组)
Description Beside other services, ACM helps companies to clearly state their “corporate identity”, ...
随机推荐
- POJ 3100:Root of the Problem
Root of the Problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12060 Accepted: 6 ...
- cordova常用命令
安装 cordova: npm install -g cordova 创建应用程序 cordova create hello com.example.hello HelloWorld 添加平台 cor ...
- 用msys1.0+mingw gcc4.7.1编译libodb(C++ ORM)
这里主要使用的是libodb 对sqlite数据库的支持. (1)下载编译sqlite3,当初编译go-sqlite3的时候下载过sqlite3,就直接拿过来用了. start msys.bat gc ...
- Python 返回多个值+Lambda的使用
def MaxMin(a,b): if(a>b): return a,b else: return b,a max,min=MaxMin(8,95) print "最大值为:" ...
- iOS核心动画以及UIView动画的介绍
我们看到很多App带有绚丽狂拽的特效,别出心裁的控件设计,很大程度上提高了用户体验,在增加了实用性的同时,也赋予了app无限的生命力.这些华丽的效果很多都是基于iOS的核心动画原理实现的,本文介绍一些 ...
- Spring MVC标签<mvc: annotation-driven />小结 原
转自:https://my.oschina.net/u/1156626/blog/881483 mvc:annotation-driven的作用 Spring 3.0.x中使用了mvc:annotat ...
- JSP共享范围
概念:对象的声明周期和可访问性称为作用域(scope). 注:有时候内置对象的作用域一旦设置就不能修改,其他对象(如JavaBean)可以设置他的作用域 作用域类型: Page:页面域(对象只对于它所 ...
- spring cloud config搭建说明例子(三)-添加actuator
添加心跳 服务端 ConfigServer pom.xml添加actuator包 <dependency> <groupId>org.springframework.cloud ...
- 笔记本 windows 10 安装
开机按快捷键是F12,选择从usb启动.秋叶系统 很好用,推荐使用. 联想笔记本u深度一键u盘启动BIOS设置教程:准备工作:制作好u深度u盘启动盘http://rj.baidu.com/soft/d ...
- jquery实现点击进入新的页面。(jquery实现超链接)
<script src="jquery-1.9.1.min.js" type="text/javascript"></script> & ...