题目链接:

Suffix

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 题意:
n个字符串每个选择一个后缀依次连接,值得新得到的字符串字典序最小; 思路:
可以发现应该从后往前,把后面得到的字符串连接到第i个后面,再求这个的最小字典序的后缀,我写后缀数组T,所以采用hash+二分寻找和当前ans的lcp,然后比较lcp的下一位更新ans AC代码:
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long LL;
const int maxn=5e5+10;
const int x=123;
char s[maxn],tep[maxn],ans[maxn];
int le[maxn],anslen,p;
LL H[maxn],xp[maxn];
inline void init()
{
xp[0]=1;
for(int i=1;i<maxn;i++)xp[i]=xp[i-1]*x;
}
int check(int len)
{
LL u=H[p]-H[p-len]*xp[len],v=H[anslen]-H[anslen-len]*xp[len];
if(u!=v)return 0;
return 1;
}
int main()
{
init();
int T;scanf("%d",&T);
while(T--)
{
int n;
scanf("%d ",&n);
int sum=0;
for(int i=1;i<=n;++i)
{
gets(s);
le[i]=strlen(s);
for(int j=0;j<le[i];j++)tep[sum+j]=s[j];
sum=sum+le[i];
}
anslen=1,p=1;
H[0]=0;ans[0]=0;
for(int i=n;i>0;i--)
{
for(int j=0;j<le[i];j++,anslen++)
{
ans[anslen]=tep[--sum];
H[anslen]=H[anslen-1]*x+(ans[anslen]-'a');
if(j==0){p=anslen;continue;}
int l=0,r=p;
while(l<=r)
{
int mid=(l+r)>>1;
if(check(mid))l=mid+1;
else r=mid-1;
}
if(l<p+1&&ans[anslen-l+1]<ans[p-l+1])p=anslen;
}
anslen=p+1;
}
for(int i=p;i>0;i--)printf("%c",ans[i]);puts("");
}
return 0;
}

  

Suffix(hash+lcp+二分)的更多相关文章

  1. Gym - 100570E:Palindrome Query (hash+BIT+二分维护回文串长度)

    题意:给定字符串char[],以及Q个操作,操作有三种: 1:pos,chr:把pos位置的字符改为chr 2:pos:问以pos为中心的回文串长度为多长. 3:pos:问以pos,pos+1为中心的 ...

  2. bzoj 1014 LCP 二分 Hash 匹配

    求同一字符串的两个后缀的最长公共前缀. 将字符串按位置放到Splay中维护(每个节点还维护一下该子树的hash),然后二分前缀的长度,用splay计算出指定范围的hash,按hash是否相等来判断是否 ...

  3. Hash(LCP) || 后缀数组 LA 4513 Stammering Aliens

    题目传送门 题意:训练指南P225 分析:二分寻找长度,用hash值来比较长度为L的字串是否相等. #include <bits/stdc++.h> using namespace std ...

  4. [poj2785]4 Values whose Sum is 0(hash或二分)

    4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 19322 Accepted: ...

  5. bzoj1014 火星人 (hash+splay+二分答案)

    求公共前缀的问题可以用hash+二分来解决,但这个是动态的,所以我们用平衡树来维护区间的hash值 复杂度$O(mlog^2n)$ #include<bits/stdc++.h> #def ...

  6. 后缀数组LCP + 二分 - UVa 11107 Life Forms

    Life Forms Problem's Link Mean: 给你n个串,让你找出出现次数大于n/2的最长公共子串.如果有多个,按字典序排列输出. analyse: 经典题. 直接二分判断答案. 判 ...

  7. nyoj--86--找球号(一)(hash&&set&&二分)

    找球号(一) 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 在某一国度里流行着一种游戏.游戏规则为:在一堆球中,每个球上都有一个整数编号i(0<=i<=10 ...

  8. 51nod(1089 最长回文子串 V2)(hash 加二分)

    1089 最长回文子串 V2(Manacher算法)   回文串是指aba.abba.cccbccc.aaaa这种左右对称的字符串. 输入一个字符串Str,输出Str里最长回文子串的长度.   输入 ...

  9. uvalive 4513 Stammering Aliens

    题意:给你一个串,问期中至少出现m次的最长子串及其起始位置的坐标. 思路:hash+LCP+二分答案 #include<cstdio> #include<cstring> #i ...

随机推荐

  1. 20144303 《Java程序设计》第五周学习总结

    20144303 <Java程序设计>第五周学习总结 教材学习内容总结 第八章 异常处理 异常就是程序在运行时出现不正常情况,异常的由来是因为Java把出现的问题封装成了对象,换句话说Ja ...

  2. Python for循环文件

    for 循环遍历文件:打印文件的每一行 #!/usr/bin/env python fd = open('/tmp/hello.txt') for line in fd: print line, 注意 ...

  3. 异步:asyncio和aiohttp的一些应用(1)

    1. asyncio 1.1asyncio/await 用法 async/await 是 python3.5中新加入的特性, 将异步从原来的yield 写法中解放出来,变得更加直观. 在3.5之前,如 ...

  4. linux 卸载jdk和安装

    卸载JDK 1.先输入java -version 查看是否安装了jdk 2.如果安装了,检查下安装的路径 which java(查看JDK的安装路径) 3.卸载 rm -rf JDK地址(卸载JDK) ...

  5. SonarQube介绍及部署

    SonarQube介绍及部署 编写人:罗旭成 编写时间:2014-5-28 SonarQube简介 SonarQube是一个用于代码质量管理的开源平台(Java开发),用于管理源代码的质量,可以从七个 ...

  6. 锁(1)-- java锁

    前言: 锁分3种:java锁.分布式锁.DB锁 在读很多并发文章中,会提及各种各样锁如公平锁,乐观锁等等,这篇文章介绍各种锁的分类.介绍的内容如下: 公平锁/非公平锁 可重入锁 独享锁/共享锁 互斥锁 ...

  7. codeforces781C Underground Lab

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  8. Codeforces Round #358 (Div. 2) C. Alyona and the Tree dfs

    C. Alyona and the Tree time limit per test 1 second memory limit per test 256 megabytes input standa ...

  9. vue中element 的上传功能

    element 的上传功能 最近有个需求,需要在上传文件前,可以进行弹窗控制是否上传upload 看完文档后,感觉有两种思路可以实现 基于before-upload :上传文件之前的钩子,参数为上传的 ...

  10. addEventListener 和 onclick 简单比较

    首先说一下addEventListener 语法: element.addEventListener(event, function, useCapture) 这里的event是事件名,functio ...