链接:https://www.nowcoder.com/acm/contest/148/J
来源:牛客网
题目描述
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 参考代码:
#include<bits/stdc++.h>
using namespace std;
vector <int> a[28];
int main()
{
int t,n,m;
cin>>t;
while(t--&&cin>>n)
{
for(int i=0; i<27; ++i) a[i].clear();
int len=0;
string str, s;
cin >> str;
for(int j=0; j<str.size(); j++) a[str[j]-'a'].push_back(len++);
rep(i,2,n)
{
cin>>s;
int cnt=-1,j;
for(j=0; j<s.size(); j++)
{
auto it = upper_bound(a[s[j]-'a'].begin(), a[s[j]-'a'].end(), cnt);
if(it==a[s[j]-'a'].end())
{
str += s.substr(j, s.size()-j);
break;
}
cnt = *it;
}
for(j; j<s.size(); j++) a[s[j]-'a'].push_back(len++);
}
cout << str << endl;
}
return 0;
}