cf比赛A题总结(输出相同前缀数量的字符串)

原题:

The length of the longest common prefix of two strings s=s1s2…sn and t=t1t2…tm is defined as the maximum integer k(0≤k≤min(n,m)) such that s1s2…sk equals t1t2…tk.

Koa the Koala initially has n+1 strings s1,s2,…,sn+1.

For each ii (1≤i≤n) she calculated aiai — the length of the longest common prefix of sisi and si+1.

Several days later Koa found these numbers, but she couldn't remember the strings.

So Koa would like to find some strings s1,s2,…,sn+1 which would have generated numbers a1,a2,…,an. Can you help her?

If there are many answers print any. We can show that answer always exists for the given constraints.

Input

Each test contains multiple test cases. The first line contains t (1≤t≤100) — the number of test cases. Description of the test cases follows.

The first line of each test case contains a single integer n (1≤n≤100) — the number of elements in the list a.

The second line of each test case contains nn integers a1,a2,…,an (0≤ai≤50) — the elements of a.

It is guaranteed that the sum of nn over all test cases does not exceed 100.

Output

For each test case:

Output n+1n+1 lines. In the ii-th line print string si (1≤|si|≤200), consisting of lowercase Latin letters. Length of the longest common prefix of strings sisi and si+1has to be equal to aiai.

If there are many answers print any. We can show that answer always exists for the given constraints.

翻译:给出一组数组,代表串字符的最大公共前缀连续长度,请输出符合条件的字符串。

例子:

input

4

4

1 2 4 2 

output

aeren
ari
arousal
around
ari

AC代码:

#include <iostream>

#define endl '\n'

using namespace std;

void solve()
{
    int n;
    cin >> n;

    string s(200, 'a');//先设置一个aaaaaaaaaaaaaaaaaa...的字符串
    cout << s << endl;//直接输出

    for (int i = 0; i < n; ++i)
    {
        int u;
        cin >> u;
        s[u] = s[u] == 'a' ? 'b' : 'a';//对第n+1个判断是否为a,为a的话就改成b,可知前面刚好有n个字符与前面的字符串相同
        cout << s << endl;//改完之后输出
    }
}

int main()
{
    int t;
    cin >> t;

    for (int i = 0; i < t; ++i)
    {
        solve();
    }

    return 0;
}

感悟:做题的时候一直在想怎么弄出来字符串,而且还要考虑前面开头字符相同的条件感觉挺复杂的,看了代码之后才知道我想的太复杂了,其实这道题只需要设一个初始字符串比如aaaaaaaa...,如果最长相同为1,那么就把第二个变成b即:abaaaaa.....,这样便可以保证输入的是n,便改变第n+1项,使前n项便相同。注意s是随着输入一个数据便改变一次,所以相同项直接改变固定位置之后直接输出就好,s为相同比较的对象自动更新。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值