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. iOS 开发之提取图片的主色调用于更换应用主题颜色

    从刷爆 IT 圈的一个事件说起: 新闻:某互联网公司产品经理提出一个需求--要求APP开发人员做到软件根据用户的手机壳改变软件的主题颜色. What Fuck!还有这操作,PM,你过来,保证不打屎你. ...

  2. SQLite3介绍

    一.SQLite数据库简介 SQLite 是一个软件库,实现了自给自足的.无服务器的.零配置的.事务性的 SQL 数据库引擎.SQLite 是在世界上最广泛部署的 SQL 数据库引擎. DDL - 数 ...

  3. 《JavaScript高级程序设计》读书笔记(四)变量、作用域和内存问题

    内容---理解基本类型和引用类型的值---理解执行环境---理解垃圾收集 --JavaScript变量松散类型的本质--决定了它只是在特定时间用于保存特定值的一个名字而已--变量的值及其数据类型可以在 ...

  4. No space left on device(总结)

    ..1 提示磁盘满了 df -hT 没有满 请问可能原因 可能是inode满了,原因是机器上的小文件太多了 使用df -hi 查看 ..2 提示没有磁盘空间已经满了 ..2.1 问题描述: 发现是日志 ...

  5. python应用-pycharm新建模板默认添加shebang编码作者时间等信息

    1.pycharm4.5激活码 用户名: yueting3527 注册码: ===== LICENSE BEGIN ===== 93347-12042010 00001FMHemWIs"6w ...

  6. tkinter的listbox、radiobutton和checkbutton学习(2)

    1.tkinter的listbox 1.1 代码 #第1步,导出模块 import tkinter as tk #定义窗口,及其标题.大小和位置 win = tk.Tk() win.title('Li ...

  7. component:(resolve) => require

    resolve => require(['../pages/home.vue'], resolve)这种写法是异步模块获取,打包的时候每次访问这个路由的时候会单调单个文件,按需加载,不过这种写法 ...

  8. I/O-<File区别>

    FileInputStream           FileOutputStream ObjectInputStream      ObjectOutputStream  传的是对象    需要新建一 ...

  9. nyoj 82

    题目:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=82 2018-06-23 23:44:05 p.s.自己省错题了: 描述 一个叫ACM的 ...

  10. [ DLPytorch ] 文本预处理&语言模型&循环神经网络基础

    文本预处理 实现步骤(处理语言模型数据集距离) 文本预处理的实现步骤 读入文本:读入zip / txt 等数据集 with zipfile.ZipFile('./jaychou_lyrics.txt. ...