Consider n given non-empty strings denoted by s1 , s2 , · · · , sn . Now for each of them, you need to select a corresponding suffix, denoted by suf1, suf2, · · · , sufn. For each string si, the suffix sufi is a non-empty substring whose right endpoint is the endpoint of the entire string. For instance, all suffixes of the string “jiangsu” are “u”, “su”, “gsu”, “ngsu”, “angsu”, “iangsu” and itself.

All selected suffixes could assemble into a long string T = suf1suf_1suf1​ + suf2suf_2suf2​ + · · · + sufnsuf_nsufn​ . Here plus signs indicate additions of strings placing the latter at the tail of the former. Your selections of suffixes would determine the lexicographical order of T . Now, your mission is to find the one with minimum lexicographical order.

Here is a hint about lexicographical order. To compare strings of different lengths, the shorter string is usually padded at the end with enough “blanks” which is a special symbol that is treated as smaller than every letters.

Input

The first line of input contains an integer T which is the total number of test cases. For each case, the first line contains an positive integer n. Each of the following n lines contains a string entirely in lowercase, corresponding to s1s_1s1​ , s2s_2s2​ , · · · , sns_nsn​ . The summation of lengths of all strings in input is smaller or equal to 500000.

Output

For each test case, output the string T with minimum lexicographical order.

样例输入

3
3
bbb
aaa
ccc
3
aba
aab
bab
2
abababbaabbababba
abbabbabbbababbab

样例输出

baaac
aaabab
aab 题意:给n个字符串,从每个字符串种选择一个最小后缀,要求把每个字符串的最小后缀拼接在一起组成一个字典序最小的字符串并输出 题解:暴力,从最后一个字符串开始处理,用for找到每个字符串的最小后缀,再用s.substr()函数拼接后缀,输出结果 注意:如果每次找到一个字符串的最小后缀,然后把所用后缀拼接起来可能并不是最终答案
比如输入2个字符串
aabaa
cc
第一个字符串的最小后缀是aa
第二个最小后缀是c
如果简单拼接,答案是aac
但真正结果是aabaac
因为后面的答案会影响前面的后缀串大小,因此我们得先求出最后的最小后缀串,然后将其加到上一个串的结尾,再求前面的串的最小的后缀串,以此类推再上一个串即可

题目来源

ACM-ICPC 2017 Asia Qingdao

/*
string s;
s.substr(start,len);//字符串提取函数,从位置start开始提取长度为len的子串
*/ #include<iostream>
#include<stdio.h>
#include<string>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N=;
string str[];
int anslen,nowlen;
int len[N];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
str[]="";
int n;
scanf("%d",&n);
for(int i=; i<=n; i++)
{
cin>>str[i];
len[i]=str[i].size();
} anslen=;
nowlen=; for(int i=n; i>=; i--)//从最后一个字符串开始处理
{
int pos=;
nowlen=str[i].size();
for(int j=; j<=len[i]; j++)//j<=len[i]保证处理每一个字符串的时候都会取一个后缀
{
if(str[i].substr(pos-,nowlen-pos+)>str[i].substr(j-,nowlen-j+) )//找到后缀最小的字符串开始的位置pos
{
pos=j;
}
}
str[i-]+=str[i].substr(pos-,nowlen-pos+);//将第i个字符串的最小后缀拼接到第i-1个字符串上
}
cout<<str[]<<endl; } return ;
}

2017 青岛现场赛 Suffix的更多相关文章

  1. 2017 青岛现场赛 I The Squared Mosquito Coil

    Lusrica designs a mosquito coil in a board with n × n grids. The mosquito coil is a series of consec ...

  2. 2018ICPC青岛现场赛 重现训练

    先贴代码,以及简要题解. 和一个队友下午双排打了一下,队友光速签到,我签的J被嫌弃写得慢以及演员...然后我秒出了E了思路然而难以置信这么简单的思路当时才过了十几个,于是发现D.F不是太好做.最后交了 ...

  3. HDU 6212 Zuma 2017青岛网络赛 区间DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6212 解法:看了眼题就发现这个BZOJ 1032不是一毛一样?但是BZOJ上那是个巨坑,数据有错,原来 ...

  4. 2018 ACM-ICPC青岛现场赛 B题 Kawa Exam 题解 ZOJ 4059

    题意:BaoBao正在进行在线考试(都是选择题),每个题都有唯一的一个正确答案,但是考试系统有m个bug(就是有m个限制),每个bug表示为第u个问题和第v个问题你必须选择相同的选项,题目问你,如果你 ...

  5. 2018ACM/ICPC 青岛现场赛 E题 Plants vs. Zombies

    题意: 你的房子在0点,1,2,3,...,n(n<=1e5)点每个点都有一颗高度为0的花,浇一次水花会长a[i]. 你有一个机器人刚开始在你家,最多走m步,每一步只能往前走或者往后走,每走到一 ...

  6. 2018 ACM-ICPC 亚洲区域赛青岛现场赛 —— Problem F. Tournament

    题面:http://acm.zju.edu.cn/contest-materials/qd2018/qd2018_problems.pdf 题意: n个骑士决斗K轮 要求是每个骑士只能跟另外一个骑士决 ...

  7. 2017南宁现场赛E The Champion

    Bob is attending a chess competition. Now the competition is in the knockout phase. There are 2^r2r  ...

  8. 2017青岛网络赛1011 A Cubic number and A Cubic Number

    A Cubic number and A Cubic Number Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/3276 ...

  9. 2017青岛网络赛1008 Chinese Zodiac

    Chinese Zodiac Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) T ...

随机推荐

  1. https://www.cnblogs.com/chanshuyi/p/alibaba_review_3_level.html

    https://www.cnblogs.com/chanshuyi/p/alibaba_review_3_level.html http://www.cnblogs.com/skywang12345/ ...

  2. maplotlib python 玩具绘图 横向纵向条状图

    from matplotlib import font_manager#解决zh-han图形汉字乱码 my_font = font_manager.FontProperties(fname=" ...

  3. SQLite3约束介绍

    SQLite 约束 约束是在表的数据列上强制执行的规则.这些是用来限制可以插入到表中的数据类型.这确保了数据库中数据的准确性和可靠性. 约束可以是列级或表级.列级约束仅适用于列,表级约束被应用到整个表 ...

  4. JS-this的使用

    做前端开发已经半年之多了,前几天看见apply时心生疑惑,于是查阅了好多资料但还是不太理解,只知道是源于this的问题,今天偶然看到了阮一峰大佬的讲解js中的this问题(http://www.rua ...

  5. Vue中修改组件默认样式

    vue 中直接使用 class 修改组件的默认样式,在使用 scoped 之后,样式是没有效果. 此时可以使用div 包裹组件,deep 可以实现修改组件样式 .lxfix /deep/ .contr ...

  6. JavaEE实战——XML文档DOM、SAX、STAX解析方式详解

    原 JavaEE实战--XML文档DOM.SAX.STAX解析方式详解 2016年06月22日 23:10:35 李春春_ 阅读数:3445 标签: DOMSAXSTAXJAXPXML Pull 更多 ...

  7. SqlHelper类编写前奏:DataReader关闭链接出现问题

    SqlHelper是一个执行数据库操作的助手类,但是当我们没学过DataSet之前,要想使用using搭配SqlConnection和SqlCommand写出一个真正独立的SqlHelper都是不太可 ...

  8. 《Web安全攻防 渗透测试实战指南》 学习笔记(一)

    Web安全攻防 渗透测试实战指南   学习笔记 (一) 第一章   信息收集     在信息收集中,最重要是收集服务器的配置信息和网站敏感信息(域名及子域名信息目标网站系统.CMS指纹.目标网站真实I ...

  9. ubuntu apache 通过端口新建多个站点

    cd /etc/apache2/sites-available 最近的虚拟机没绑定域名,所以呢,就先用域名加端口新建几个站点用着 1. vim /etc/apapche2/apapche2.conf ...

  10. 解决小程序报错 Page "pages/index/main" has not been registered yet.

    在小程序开发中,会频繁遇到  Page "pages/index/main" has not been registered yet.   这种报错,意思就说指定的页面没有注册,找 ...