题目链接:

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. swoole 异步队列简明教程

    安装步骤如下(推荐把安装文件下载到 /usr/local/src 目录下): step 1: wget --no-check-certificate https://github.com/swoole ...

  2. CentOS7系统安装及环境初始化

    操作系统安装:    将网卡名称设置为eth*,不使用CentOS 7默认的网卡命名规则.所以需要在安装的时候,增加内核参数.1. 光标选择“Install CentOS 7” 2. 点击Tab,打开 ...

  3. js去除前后空格

    <script language="javascript"> String.prototype.trim=function(){     return this.rep ...

  4. mysql常见知识点总结

    mysql常见知识点总结 参考: http://www.cnblogs.com/hongfei/archive/2012/10/20/2732516.html https://www.cnblogs. ...

  5. 使用WebUploader实现文件批量上传,进度条显示功能

    知识点:利用WebUploader,实现文件批量上传,并且实时显示文件的上传进度 参考官方文档:http://fex.baidu.com/webuploader/ (1)引入三个资源 JS,CSS,S ...

  6. C#(Wpf)实现小键盘

    花了一天时间小键盘基本功能已完成,先看看效果图吧! 默认: Shift: Caps Lock: Button style <Style x:Key="KeyButton" T ...

  7. vue element new vue const

    new Vue{ el:"app", } ========= const app = new Vue({ router, data:{....} }).$mount('#app') ...

  8. Qwt中鼠标获取坐标点

    void getPoint(QwtPlot *plot) { QPoint point = plot->canvas()->mapFromGlobal(QCursor::pos()); Q ...

  9. C++11 auto类型说明符的使用

    编程的时候常常需要把表达式的值赋给变量,这就要求在声明变量的时候清楚地知道表达式的类型.然而做到这一点很难,有时候根本做不到.为了解决这个问题.C++11新标准引入了auto类型说明符,用它就 能让编 ...

  10. PHP表单(get,post)提交方式

    PHP 表单处理 PHP 超全局变量 $_GET 和 $_POST 用于收集表单数据(form-data). $_GET 是通过 URL 参数传递到当前脚本的变量数组. $_POST 是通过 HTTP ...