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. c语言中的const的作用及解析

    有时候我们希望定义这样一种变量,它的值不能被改变,在整个作用域中都保持固定.例如,用一个变量来表示班级的最大人数,或者表示缓冲区的大小.为了满足这一要求,可以使用const关键字对变量加以限定: co ...

  2. 【ionic App问题总结系列】ionic 如何更新app版本

    ionic 如何进行自动更新 ionic App更新有两种方式:第一种是普通的从远程下载apk,安装并覆盖旧版本.另外一种就是采用替换www文件夹的内容,实现应用内更新,而无需下载安装apk. 这篇文 ...

  3. http respose status code (记)

    1xx - 信息提示这些状态代码表示临时的响应.客户端在收到常规响应之前,应准备接收一个或多个 1xx 响应. · 100 - Continue 初始的请求已经接受,客户应当继续发送请求的其余部分.( ...

  4. 在GridView控件FooterTemplate内添加记录 Ver2

    中午有发表一篇博文<在GridView控件FooterTemplate内添加记录> http://www.cnblogs.com/insus/p/3269908.html 添加铵钮是放在F ...

  5. jmeter 阶梯式加压测试

    性能测试中,有时需要模拟一种实际生产中经常出现的情况,即:从某个值开始不断增加压力,直至达到某个值,然后持续运行一段时间. 在jmeter中,有这样一个插件,可以帮我们实现这个功能,这个插件就是:St ...

  6. 入门GitHub

    Step 1: 创建一个我们自己的账号 我们先登录https://github.com,然后单击sign up for Github,我们输入用户名,密码和邮箱就可以有一个 属于我们自己的Github ...

  7. Go语言技术教程:Redis介绍安装和使用

    Redis介绍 我们日常的开发,数据都需要进行持久化存储,常见的持久化存储有很多种,比如数据库,文件,计算机内存,甚至云服务器等都是持久化存储数据的方式.而就数据库而言,经常又会被人们分为关系型数据库 ...

  8. P3321 [SDOI2015]序列统计 FFT+快速幂+原根

    \(\color{#0066ff}{ 题目描述 }\) 小C有一个集合S,里面的元素都是小于M的非负整数.他用程序编写了一个数列生成器,可以生成一个长度为N的数列,数列中的每个数都属于集合S.小C用这 ...

  9. 模板 可并堆【洛谷P3377】 【模板】左偏树(可并堆)

    P3377 [模板]左偏树(可并堆) 如题,一开始有N个小根堆,每个堆包含且仅包含一个数.接下来需要支持两种操作: 操作1: 1 x y 将第x个数和第y个数所在的小根堆合并(若第x或第y个数已经被删 ...

  10. redis分布式锁的使用

    一  本身自带的方法进行使用: <dependency> <groupId>redis.clients</groupId> <artifactId>je ...