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 = suf_1suf​1​​ + suf_2suf​2​​ + · · · + suf_nsuf​n​​ . 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 s_1s​1​​ , s_2s​2​​ , · · · , s_ns​n​​ . 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

题目来源

ACM-ICPC 2017 Asia Qingdao

https://nanti.jisuanke.com/t/18520

考虑到,每一个字符串起码要选一个后缀,也就是每个字符串的最后一个字符是必须要的

那么,从最后一个字符串开始搞起,每次都从一个字符串的倒数第二个字符开始插入(倒数第一个必须要插入)

然后就相当于给你一个字符串,找出字典序最小的后缀。

设答案后缀下标是ansp,每次插入一个,就需要比较suffix(ansp)和suffix(now)的字典序大小

hash,二分lcp,判断下一位字母大小即可。

复杂度nlogn

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef unsigned long long int LL;
const int maxn = + ;
string str[maxn];
char ans[maxn];
unsigned long long int sum[maxn], po[maxn];
const int seed = ;
int len[maxn];
bool check(int one, int ansp) {
int be = , en = ansp;
while (be <= en) {
int mid = (be + en) >> ;
if (sum[one] - sum[one - mid] * po[mid] == sum[ansp] - sum[ansp - mid] * po[mid]) {
be = mid + ;
} else en = mid - ;
}
// printf("%d %d\n", be, en);
if (be == ansp + ) return false;
return ans[one - en] < ans[ansp - en];
}
char fuck[maxn];
void work() {
int n;
scanf("%d", &n);
for (int i = ; i <= n; ++i) {
// cin >> str[i];
scanf("%s", fuck);
str[i] = string(fuck);
len[i] = strlen(str[i].c_str());
}
int ansp = ;
for (int i = n; i >= ; --i) {
ans[++ansp] = str[i][len[i] - ];
sum[ansp] = sum[ansp - ] * seed + str[i][len[i] - ]; //
int to = ;
int t = ansp;
for (int j = len[i] - ; j >= ; --j) {
ans[ansp + to] = str[i][j];
sum[ansp + to] = sum[ansp + to - ] * seed + str[i][j];
if (check(ansp + to, t)) {
t = ansp + to;
}
to++;
}
ansp = t;
// for (int j = ansp; j >= 1; --j) {
// printf("%c", ans[j]);
// }
// printf("\n");
}
for (int i = ansp; i >= ; --i) {
printf("%c", ans[i]);
}
printf("\n");
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
po[] = ;
for (int i = ; i <= maxn - ; ++i) {
po[i] = po[i - ] * seed;
}
int t;
scanf("%d", &t);
while (t--) work();
return ;
}

suffix ACM-ICPC 2017 Asia Qingdao的更多相关文章

  1. ACM ICPC 2017 Warmup Contest 9 I

    I. Older Brother Your older brother is an amateur mathematician with lots of experience. However, hi ...

  2. ACM ICPC 2017 Warmup Contest 9 L

    L. Sticky Situation While on summer camp, you are playing a game of hide-and-seek in the forest. You ...

  3. ACM ICPC 2017 Warmup Contest 1 D

    Daydreaming Stockbroker Gina Reed, the famous stockbroker, is having a slow day at work, and between ...

  4. 2017 ACM/ICPC Asia Regional Qingdao Online

    Apple Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submi ...

  5. 2016 ACM/ICPC Asia Regional Qingdao Online 1001/HDU5878 打表二分

    I Count Two Three Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  6. HDU 5889 Barricade 【BFS+最小割 网络流】(2016 ACM/ICPC Asia Regional Qingdao Online)

    Barricade Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  7. 2016 ACM/ICPC Asia Regional Qingdao Online(2016ACM青岛网络赛部分题解)

    2016 ACM/ICPC Asia Regional Qingdao Online(部分题解) 5878---I Count Two Three http://acm.hdu.edu.cn/show ...

  8. 2017 ACM/ICPC Asia Regional Shenyang Online spfa+最长路

    transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/1 ...

  9. 2017 ACM ICPC Asia Regional - Daejeon

    2017 ACM ICPC Asia Regional - Daejeon Problem A Broadcast Stations 题目描述:给出一棵树,每一个点有一个辐射距离\(p_i\)(待确定 ...

随机推荐

  1. CodeForces 402D Upgrading Array (数学+DP)

    题意:给出一个数列,可以进行一种操作将某一个前缀除去他们的gcd,有一个函数f(x),f(1) = 0 , f(x) = f(x/p)+1,f(x) = f(x/p)-1(p是坏素数), 求 sum( ...

  2. url-pattern 的设置与匹配

  3. 用create table 命令建立表

    create table [[V.]HANKE.].MADE IN HOME (xuliehao int primary key, name varchar(20)not null, jiage fl ...

  4. SQLServer存储引擎——01.数据库如何读写数据

    一.引言 在SQL Server数据库中,数据是如何被读写的?日志里都有些什么?和数据页之间是什么关系?数据页又是如何存放数据的?索引又是用来干嘛的? 一起看看SQL Server的存储引擎. 二.S ...

  5. 在GridView控件FooterTemplate内添加记录

    在GridView控件FooterTemplate内添加记录,想实现这个功能,有几点要清楚的,这个添加铵钮是在FooterTemplate内,还是在GridView控件外部,位置不同,某些处理逻辑会有 ...

  6. code manager tools myeclipse10 svn插件安装及使用

    一.下载: 1.在线安装地址:http://subclipse.tigris.org/update_1.6.x 2.安装包下载地址:http://subclipse.tigris.org/servle ...

  7. 为所有的Ul下的li标签添加点击事件

  8. 设置SQL脚本大小敏感

    1.设置SQL脚本大小写不敏感 USE [master]  GO  ALTER DATABASE [DatabaseName] COLLATE Chinese_PRC_CI_AI  GO 2.设置大S ...

  9. CoreAnimation 核心动画 / CABasicAnimation/ CAKeyframeAnimation

    - (void)createBaseAnimation{ //基础动画 CABasicAnimation *animation = [CABasicAnimation animation]; anim ...

  10. 树莓派安装开源智能家居系统 Domoticz

    前言 最近闲来无事开始折腾自己的智能家居系统,对比了几种比较流行的开源智能家居系统,觉得 Domoticz 更适合,Domoticz的官方中文文档,虽然不是很完善但还是可以参考一下.需要注意的是下文用 ...