Rikka with Nickname

来源:牛客网
题目链接:J-Rikka with Nickname_2022牛客五一集训派对day3 (nowcoder.com)

题目描述

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 a1...a∣s∣(1≤a1<a2<...<a∣s∣≤∣r∣)a_1...a_{|s|}( 1 \leq a_1 < a_2 < ... < a_{|s|} \leq |r|)a1​...a∣s∣​(1≤a1​<a2​<...<a∣s∣​≤∣r∣) which satisfies si=rais_i = r_{a_i}si​=rai​​.

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.

 链接:登录—专业IT笔试面试备考平台_牛客网
来源:牛客网
 

输入描述:

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 si(1≤∣si∣≤106,∑i=1n∣si∣≤106)s_i(1 \leq |s_i| \leq 10^6, \sum_{i=1}^n |s_i| \leq 10^6)si​(1≤∣si​∣≤106,∑i=1n​∣si​∣≤106).

输出描述:

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

思路:

本题大意是将字符串连接起来,并符合子串的规律(从头开始,不限位置),没有就直接连接.直接判断后面的字符串从头开始判断有没有与之前的字符串相同的字符,有的话两个字符串同时向后看,没有就前一个字符向后看,直到查询结束,然后将剩下的连接到一起。

【(废话)刚开始没把题目的意思理解清楚,还以为是把后一个字符串中第一个的前一个字符串的最后一个字符往后,连接到前一个字符串中,结果不仅wa了还超时了(sad),后面理清楚了,就ac了,这就是英语渣的下场,只能靠猜题目的意思,5555】

下面就是ac代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
	ll n,t,i,l,j,k;
	static string a,b;
	cin>>n;
	while(n--)
	{
		cin>>t;
		cin>>a;
		for(i=1;i<t;i++)
		{
			cin>>b;
			j=0,k=0;
			while(j<a.length()&&k<b.length())
			{
				if(a[j]==b[k])
				k++;
				j++;
			}
			a=a+b.substr(k);
		}
		cout<<a<<endl;
	 } 
 } 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值