Codeforces Round #659 (Div. 2) A. Common Prefixes(思维)
The length of the longest common prefix of two strings \(s=s_1 s_2…s_n\) and \(t=t_1t_2…t_m\) is defined as the maximum integer k0≤k≤min(n,m)) such that \(s=s_1 s_2…s_n\) equals to \(t=t_1t_2…t_m\)
Koa the Koala initially has n+1 strings \(s=s_1 s_2…s_{n+1}\)
For each i (1≤i≤n) she calculated \(a_i\)— the length of the longest common prefix of \(s_i\) and \(s_{i+1}\) .
Several days later Koa found these numbers, but she couldn't remember the strings.
So Koa would like to find some strings \(s_1, s_2,…s_{n+1}\)which would have generated numbers \(a_1, a_2,…a_n\). Can you help her?
If there are many answers print any. We can show that answer always exists for the given constraints.
Input
Output
Example
Input
Copy
4
4
1 2 4 2
2
5 3
3
1 3 1
3
0 0 0
Output
Copy
aeren
ari
arousal
around
ari
monogon
monogamy
monthly
kevinvu
kuroni
kurioni
korone
anton
loves
adhoc
problems
首先随便构造一个字符串作为第一个,然后对于每个\(a_i\),把上一个字符串下标为\(a_i\)的字母换掉就得到当前字符串了(可以直接ASCII码减去'a'再加1,再对26取模)。
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
string s = "";
for(int i = 0; i < 200; i++) s += 'a';
cout << s << endl;
for(int i = 1; i <= n; i++)
{
int temp;
cin >> temp;
s[temp] = (s[temp] - 'a' + 1) % 26 + 'a';
cout << s << endl;
}
}
return 0;
}