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. hamming distance(汉明距离)

    看knn算法时无意间发现这个算法,但是维基上有错误的示例和python代码...因为汉明距离并不是求相同长度字符串(或相同长度的整数)之间的字符(或数位)差异个数. 正确的详见:https://en. ...

  2. linux 开启普通用户sudo root权限操作获取免密

    root 身份登陆 $ visudo然后进入修改配置找到 root    ALL=(ALL) ALL 在下面增加: yourusername ALL=(ALL)   NOPASSWD:  ALL ex ...

  3. cnblogs 自定义主题字体渲染方案

    渲染效果图 由于我一直偏好衬线字体,所以在采用 Silence 主题 之后,还参照谢益辉的博客字体方案进行了改进 首先,在页首代码中添加盘古之白,如果你同时编写 中/英 文博客,你当然应该学习谢益辉的 ...

  4. TCP 连接建立分析

    tcp 三次握手与四次挥手 tcp 报文结构 tcp 是全双工的,即 client 向 server 发送信息的同时,server 也可以向 client 发送信息. 在同主机的两个 session ...

  5. 使用电脑热点和Fiddler对Android app进行抓包

    如果没有路由器,怎么对app抓包?如果你的电脑可以开热点的话也可以. 打开Fiddler,菜单栏选择Tools->Options->Connections,勾选Allow remote c ...

  6. 跨域-JSONP

    jsonp跨域 - 前端适配,后台配合 说明:前后台同时改造 cnpm i jsonp --save-dev 在App.vue里 import jsonp from 'jsonp' let url = ...

  7. SpringBoot Profiles特性

    今天我们了解SpringBoot Profiles特性 一.外部化配置  配置分为编译时和运行时,而Spring采用后者,在工作中有时也会两者一起使用.  何为"外部化配置"官方没 ...

  8. Python:时间日历基本处理

    time 模块 提供了处理时间和表示之间转换的功能 获取当前时间戳 时间戳:从0时区的1970年1月1日0时0分0秒,到所给定日期时间的时间,浮点秒数,或者毫秒整数 获取方式: import time ...

  9. Java 11 New Features

    前言 北京时间 2018年9 月 26 日,Oracle 官方宣布 Java 11 正式发布.这是 Java 大版本周期变化后的第一个长期支持版本,非常值得关注.从官网即可下载, 最新发布的 Java ...

  10. ubuntu16.04安装node.js、npm

    ubuntu16.04安装node.js.npm1.请尽量避免在 Ubuntu 上使用 apt-get 来安装 node.js, 如果你已经这么做了,请手动移除: sudo apt-get purge ...