Codeforces Round 506 (Div. 3) 题解

A

题目链接

观察样例可以发现,只要找到最长的和后缀相同的前缀,然后重复该前缀$k$次,再输出剩下的即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cctype>
#define INF 2000000000
using namespace std;
typedef long long ll;
int read(){
int f = 1, x = 0;
char c = getchar();
while(c < '0' || c > '9'){if(c == '-') f = -f; c = getchar(); }
while(c >= '0' && c <= '9')x = x * 10 + c - '0', c = getchar();
return f * x;
}
int n, k;
char s[105];
void init(){
n = read(), k = read();
scanf("%s", s);
}
void solve(){
int st;
for(st = 1; st < n; st++){
int flag = 1;
for(int j = 0; j < n - st; ++j)
if(s[st + j] != s[j]){
flag = 0;
break;
}
if(flag) break;
}
if(st == n){
for(int i = 0; i < k; ++i)
printf("%s", s);

}else{
for(int i = 0; i < k; ++i)
for(int j = 0; j < st; ++j)
putchar(s[j]);
for(int i = st; i < n; ++i)
putchar(s[i]);
}
printf("\n");
}
int main(){
init();
solve();
return 0;
}


B

题目链接


C

题目链接