A. Many Equal Substrings

题目链接:https://codeforces.com/contest/1029/problem/A
A. Many Equal Substrings
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a string t consisting of n lowercase Latin letters and an integer number k.
Let’s define a substring of some string s with indices from l to r as s[l…r].
Your task is to construct such string s of minimum possible length that there are exactly k positions i such that s[i…i+n−1]=t. In other words, your task is to construct such string s of minimum possible length that there are exactly k substrings of s equal to t.
It is guaranteed that the answer is always unique.
Input
The first line of the input contains two integers n and k (1≤n,k≤50) — the length of the string t and the number of substrings.
The second line of the input contains the string t consisting of exactly n lowercase Latin letters.

Output
Print such string s of minimum possible length that there are exactly k substrings of s equal to t.
It is guaranteed that the answer is always unique.

Examples
input
3 4
aba
output
ababababa
input
3 2
cat
output
catcat

题意:两个整数n,k,一个字符串s。n代表子串的长度,k代表子串的数量;
然后,构造这样一个最小可能长度的字符串s,正好有k个位置i,使得s [i … i + n-1] = t。换句话说,你的任务是构造这样一个最小可能长度的字符串s,正好有k个子字符串s等于t。
保证答案始终是唯一的。
解题思路:找出s中相同的前缀和后缀相同的最大长度h然后先打印一个s,再打印k-1个h;

#include<iostream>
#include<string>
using namespace std;
int main(){
	int n,k;
	string str;
	cin>>n>>k>>str;
	int pos=0;
	for(int i=0;i<n;i++)  // 找出前缀和后缀相同的最大长度 
		if(str.substr(0,i)==str.substr(n-i,i)) pos=i;
	cout<<str;
	for(int i=1;i<k;i++)
		cout<<str.substr(pos);
	cout<<endl;
	return 0;
}

翻译:
给你一个由n个小写拉丁字母和一个整数k组成的字符串t。
让我们定义一些字符串s的子字符串,索引从l到r为s [l … r]。
你的任务是构造这样一个最小可能长度的字符串s,正好有k个位置i,使得s [i … i + n-1] = t。换句话说,你的任务是构造这样一个最小可能长度的字符串s,正好有k个子字符串s等于t。
保证答案始终是唯一的。
输入
输入的第一行包含两个整数n和k(1≤n,k≤50) - 字符串t的长度和子串的数量。
输入的第二行包含由正好n个小写拉丁字母组成的字符串t。

产量
打印这样一个最小可能长度的字符串s,正好有k个子字符串s等于t。
保证答案始终是唯一的。

全部评论

相关推荐

03-06 12:44
已编辑
吉林大学 Java
是个千人厂,没听过名字。1.&nbsp;做一个自我介绍。2.&nbsp;你这个项目和技术栈从哪里学的?有报辅导班嘛[答&nbsp;都是是自己网上学的,学校教的东西没用]3.&nbsp;我看了你放在github上的项目,前端也是你写的嘛[答&nbsp;AI写的,90%精力用于后端开发,前端单纯用于作为后端逻辑的可视化技术验证(骗你的其实后端也是AI写的)]4.&nbsp;好,你觉得这些技术栈研究得最深刻的是哪个[答&nbsp;八股压根没背到后面,昨晚背了MySQL就说MySQL]5.&nbsp;那讲一下MySQL的索引[答&nbsp;从B+树选型一路吟唱到联合索引,索引失效]6.&nbsp;联合索引ABC问题,AB走索引嘛,BC走索引嘛?BAC走索引嘛?A&nbsp;or&nbsp;B&nbsp;走索引嘛[走,不走,走,不走。面试官点头说可以]7.&nbsp;讲一下项目里Redission分布式锁实现8.&nbsp;Watchdog机制具体是怎么工作9.&nbsp;消息队列有考虑过Kafka嘛,怎么选型的10.&nbsp;你这个项目消息队列可能出现什么问题,怎么解决这个问题?[瞎扯没用的,被面试官引导答了视频处理可能产生消息堆积问题,然后开始吟唱]11.&nbsp;文件分片自己写的还是用的什么框架?上传进度的Redis数据结构?上传的视频有多大?小分片大小?12.&nbsp;项目里Redis会话记忆是啥意思?[面试官说不行,没人把这个全放Redis里[生气R]]13.&nbsp;那这和直接查数据库有什么区别[扯了Token成本和解决幻觉问题之类的,给面试官听笑了,我最后也没绷住]14.&nbsp;你平时是怎么使用AI&nbsp;coding的15.&nbsp;算法,给了我一个leedcode链接,一看做过了。然后换了一道三数之和,也做过了。然后面试官说算了,让我讲讲思路吧反问:1.有什么需要提高的地方2.介绍一下部门业务有哪些这个面试官真的感官非常非常好,问问题还疯狂引导,感觉不会也会了。找实习&nbsp;&nbsp;牛客AI配图神器#
查看15道真题和解析
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务