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。
保证答案始终是唯一的。

猜你喜欢

转载自blog.csdn.net/qq_43516113/article/details/89047761