Rikka with Nickname

本文介绍了一个将多个字符串按特定规则合并成单个字符串的算法,该算法用于生成更优的昵称。通过实例展示了如何逐步将字符串融合到一起,无法融合的部分则追加到最终字符串的末尾。

链接:https://www.nowcoder.com/acm/contest/148/J
来源:牛客网
 

时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

Sometimes you may want to write a sentence into your nickname like "lubenwei niubi". But how to change it into a single word? Connect them one by one like "lubenweiniubi" looks stupid.

To generate a better nickname, Rikka designs a non-trivial algorithm to merge a string sequence s1...sn into a single string. The algorithm starts with s=s1 and merges s2...sn into s one by one. The result of merging t into s is the shortest string r which satisfies s is a prefix of r and t is a subsequence of r.(If there are still multiple candidates, take the lexicographic order smallest one.)

String s is a prefix of r if and only if |s| ≤ |r| and for all index i ∈ [1, |s|], si = ri.

String s is a subsequence of r if and only if there is an index sequence which satisfies .

For example, if we want to generate a nickname from "lubenwei niubi", we will merge "niubi" into "lubenwei", and the result is "lubenweiubi".

Now, given a sentence s1...sn with n words, Rikka wants you to calculate the resulting nickname generated by this algorithm.

输入描述:

The first line contains a single number t(1 ≤ t ≤ 3), the number of testcases.

For each testcase, the first line contains one single integer n(1 ≤ n ≤ 106).

Then n lines follow, each line contains a lowercase string .

输出描述:

For each testcase, output a single line with a single string, the result nickname.

 

示例1

输入

2
2
lubenwei
niubi
3
aa
ab
abb

输出

lubenweiubi
aabb

题解:从第一个字符串开始,第二个字符串以顺序的方式融入前一个字符串,融不进的就把剩下的字符串添加在总字符串尾;

           很简单,想复杂了怎么也搞不出来;

tips:string定义的字符串变量可以直接加;

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t,n;
    string s,s1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        cin>>s;
        for(int i=1;i<n;i++)
        {
            cin>>s1;
            int k=0,j=0;
            while(s[j]&&s1[k])
            {
                if(s[j]==s1[k])
                {
                    k++;
                }
                j++;
            }
            while(s1[k])
            {
                s+=s1[k];//s字符串变量可以用加的方式把字符直接加在末尾
                k++;
            }
            s1="";//定义s1字符串为空
        }
        cout<<s<<endl;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值