suffix ACM-ICPC 2017 Asia Qingdao
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_1suf1 + suf_2suf2 + · · · + suf_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 s_1s1 , s_2s2 , · · · , s_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
题目来源
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的更多相关文章
- ACM ICPC 2017 Warmup Contest 9 I
I. Older Brother Your older brother is an amateur mathematician with lots of experience. However, hi ...
- 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 ...
- ACM ICPC 2017 Warmup Contest 1 D
Daydreaming Stockbroker Gina Reed, the famous stockbroker, is having a slow day at work, and between ...
- 2017 ACM/ICPC Asia Regional Qingdao Online
Apple Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submi ...
- 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 ...
- 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 ...
- 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 ...
- 2017 ACM/ICPC Asia Regional Shenyang Online spfa+最长路
transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 132768/1 ...
- 2017 ACM ICPC Asia Regional - Daejeon
2017 ACM ICPC Asia Regional - Daejeon Problem A Broadcast Stations 题目描述:给出一棵树,每一个点有一个辐射距离\(p_i\)(待确定 ...
随机推荐
- Entity Framework Tutorial Basics(16):Linq-to-Entities Projection Queries
Linq-to-Entities Projection Queries: Here, you will learn how to write LINQ-to-Entities queries and ...
- backstop bsg6
- C++面试笔记--继承和接口
整个C++程序设计全面围绕面向对象的方式进行.类的继承特性是C++的一个非常重要的机制.继承特性可以使一个新类获得其父类的操作和数据结构,程序员只需在新类中增加原有类没有的成分. 在面试过程中,各大企 ...
- 《Head First Servlets & JSP》-8-无脚本的JSP
以前servlet和JSP交互的代码 servlet代码示例: JSP代码示例: 若属性不是一个String而是一个Bean呢? 一个简单的JavaBean servlet代码示例: JSP代码示例: ...
- 关于C#中的算术运算
使用中间变量交换两个int型变量的值: ; ; a = a+b; b = a-b; a = a-b; 相信大家很容易写出来,但考虑到边界值情况时会有一些有趣的事情. 我们知道有一个int.MaxVal ...
- vtk-py求3d模型表面积
模型格式:.obj 环境:python3.6+vtk7.1 vtk版: import vtk filename = "XXXX.obj" reader = vtk.vtkOBJRe ...
- t-sql read xlsx
How to Read and Load an Excel 2007 or Excel 2010 File Without Using Import/Export Utility To read an ...
- windows下eclipse远程连接hadoop集群开发mapreduce
转载请注明出处,谢谢 2017-10-22 17:14:09 之前都是用python开发maprduce程序的,今天试了在windows下通过eclipse java开发,在开发前先搭建开发环境.在 ...
- [SCOI2007]蜥蜴 BZOJ1066 最大流
题目背景 07四川省选 题目描述 在一个r行c列的网格地图中有一些高度不同的石柱,一些石柱上站着一些蜥蜴,你的任务是让尽量多的蜥蜴逃到边界外. 每行每列中相邻石柱的距离为1,蜥蜴的跳跃距离是d,即蜥蜴 ...
- C语言使用指针表示数组的注意事项
1)数组名是指针常量 如对指针变量可以进行++运算,但是对数组名却不允许,另外,对数组名的赋值运算也是错误的 2)注意指针变量的当前值 指针变量的值在程序运行过程中可能经常改变,要对此注意 3)数组越 ...