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”, ...
随机推荐
- ubuntu-10.10嵌入式开发环境搭建【转】
本文转载自:http://blog.csdn.net/zjhsucceed_329/article/details/8036781 版权声明:本文为博主原创文章,未经博主允许不得转载. ubuntu- ...
- What does jQuery.fn mean?
n jQuery, the fn property is just an alias to the prototype property. The jQuery identifier (or $) i ...
- YTU 2642: 填空题:类模板---求数组的最大值
2642: 填空题:类模板---求数组的最大值 时间限制: 1 Sec 内存限制: 128 MB 提交: 646 解决: 446 题目描述 类模板---求数组的最大值 找出一个数组中的元 ...
- leetcode 784. Letter Case Permutation——所有BFS和DFS的题目本质上都可以抽象为tree,这样方便你写代码
Given a string S, we can transform every letter individually to be lowercase or uppercase to create ...
- luogu 3953 逛公园
noip2017 D1T3 逛公园 某zz选手看到数据范围直接就最短路计数了,结果写错了爆零 题目大意: N个点M条边构成的有向图,且没有自环和重边.其中1号点是起点,N号点是公园的终点,每条边有一个 ...
- codeforces 965E Trie+multiset
E. Short Code time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 公司6:JrVue重用布局
JrVue是我们基于element重新封装的一套组件库; 为了提高开发效率, 方便对代码的统一管理和维护, 我们提供了一些统一的页面布局, 以及项目常用的中小型模块 我在master新拉一个分支J ...
- C#自动内存分配
一.在程序运行时由CLR管理内存分配(Memory Allocation),程序启动时,操作系统会为每一个线程申请一个独立的栈内存,用于存储方法的局部变量.参数和返回值等:CLR会为进程申请一个连续的 ...
- 数学+DP Codeforces Round #304 (Div. 2) D. Soldier and Number Game
题目传送门 /* 题意:这题就是求b+1到a的因子个数和. 数学+DP:a[i]保存i的最小因子,dp[i] = dp[i/a[i]] +1;再来一个前缀和 */ /***************** ...
- 数据传递-------@ModelAttribute
package com.wh.handler; /** * @ModelAttribute绑定请求参数到命令对象 * @ModelAttribute一个具有如下三个作用: * * ①绑定请求参数到命令 ...