题目链接:

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. oppo R9 WLAN使用代理图解

    以上拼图便是oppo R9 WLAN使用代理图解,代理设为 '手动' ,主机名便是我的电脑的ip地址,端口号是9973: + 9973端口号 (微信web开发者工具不可更改): + 8888 端口号 ...

  2. Python3.6 安装、后续终端pip 安装模块命令

    1. 下载安装包 https://www.python.org/ftp/python/3.6.4/python-3.6.4-amd64.exe 2. 安装python3.6 增加环境变量(打钩.红框很 ...

  3. Hive-0.13安装

    Hive只需在使用节点安装即可. 1.上传tar包.解压   tar -zxvf apache-hive-0.13.0-bin.tar.gz -C /hadoop/  配置HIVE_HOME环境变量  ...

  4. MysQL使用一创建库与表

    数据库简介 人类在进化的过程中,创造了数字.文字.符号等来进行数据的记录,但是承受着认知能力和创造能力的提升,数据量越来越大,对于数据的记录和准确查找,成为了一个重大难题 计算机诞生后,数据开始在计算 ...

  5. mysql数据库无法连接(JDBC)java.net.ConnectException: Connection timed out

    数据库无法连接(JDBC) 用户名密码正确,但是一直报错:Connection timed out 后来知道了原因:我用的是BAE提供的云mysql数据库,对访问的IP有限制 ,所以在本机上无法连接. ...

  6. DOS/BAT批处理if exist else 语句的几种用法

    在DOS批处理命令中常常会通过if语句来进行判断来执行下面的命令, 那么批处理if语句怎么用呢,下面学无忧小编就来说说有关批处理if以及if exist else语句的相关内容.一.批处理if书写格式 ...

  7. 秒懂算法3——插入排序(C#实现)

    算法思路: 将n个元素分成[已排序]和[未排序]两部分.每次将[未排序]中的一个元素取出,插入到已排序中的相应位置.直至所有元素排序完毕. [已排序] [未排序] { { a[0] }         ...

  8. C++学习——C++复合类型

    1.引用 引用是为某一个变量起了另一个名字,定义方式为type &rval = val; 引用类型必须与引用的变量类型完全一致,引用后,rval和val将会被视为一个变量,只不过有两种调用方式 ...

  9. git命令速记

    对于不常写代码,有的时候又要提交点代码的人来说,git命令记了又忘,忘了又去花精力记住.有没有一种速记方法,来帮助我们记忆这些玩意呢? 纯属抄袭@_@! 除了几个很基本的命令,复杂一点的,我都要去查, ...

  10. Android----Material Design之(FloatActionButton,CoordinatorLayout,CollapsingToolbarLayout,AppBarLayout,TabLayout等)

    Material Design 的一些UI 平常开发还是用的比较多的,以前没写,最近总结一下,写一篇博客,要求版本在5.0以上. 主要介绍了FloatActionButton,CoordinatorL ...