Suffix

简介: 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 = suf1+ suf2+ · · · +sufn

. 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 s1, s2, · · · , s 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.

题意:给你n个字符串,你必须按照输入顺序在每个字符串里取一个后缀子串,然后拼接起来,使得其字典序最小,并输出拼接后的字符串。


思路:思路:咋一看好像对每一个字符串求一次sa数组,找到排行第一的那个后缀串,然后拼接起来就ok,但是实际一想,显然不是,比如aabaa cc,如果按照上面的算法,得到aac,但是答案却是aabaac,因为后面的答案会影响前面的后缀串大小,因此我们得先求出最后的最小后缀串,然后将其加到上一个串的结尾,再求前面的串的sa数组再去找最小的后缀串,以此类推再上一个串即可。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 5;
char s[maxn], ans[maxn];
int tx[maxn];
int sa[maxn], rak[maxn];
string str[maxn];
int n, m, p, cnt;
struct node {
  int x, y, id;
}a[maxn], b[maxn];
void rsort() {
  for (int i = 1; i <= m; i++) {
    tx[i] = 0;
  }
  for (int i = 1; i <= n; i++) {
    tx[a[i].y]++;
  }
  for (int i = 1; i <= m; i++) {
    tx[i] += tx[i - 1];
  }
  for (int i = 1; i <= n; i++) {
    b[tx[a[i].y]--] = a[i];
  }
  for (int i = 1; i <= m; i++) {
    tx[i] = 0;
  }
  for (int i = 1; i <= n; i++) {
    tx[b[i].x]++;
  }
  for (int i = 1; i <= m; i++) {
    tx[i] += tx[i - 1];
  }
  for (int i = n; i >= 1; i--) {
    a[tx[b[i].x]--] = b[i];
  }
}
void ssort() {
  rsort();
  p = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i].x != a[i - 1].x || a[i].y != a[i - 1].y) {
      ++p;
    }
    rak[a[i].id] = p;
  }
  for (int i = 1; i <= n; i++) {
    a[i].x = rak[i];
    a[i].id = sa[rak[i]] = i;
    a[i].y = 0;
  }
  m = p;
}
void solve(int len) {
  m = 127;
  n = min(3 * len + 27, cnt); //注意27是必须的,否则会wa掉(我也不知道为什么一定要+27
  for (int i = 1; i <= n; i++) {
    s[i] = ans[cnt - i + 1];
    a[i].x = a[i].y = s[i];
        a[i].id = i;
  }
  ssort();
  for (int j = 1; j <= n; j <<= 1) {
    for (int i = 1; i + j <= n; i++) {
      a[i].y = a[i + j].x;
    }
    ssort();
    if (p == n) {
      break;
    }
  }
}
int main() {
  int T, q;
  ios::sync_with_stdio(false);
  cin.tie(0); cout.tie(0);
  cin >> T;
  while (T--) {
    cin >> q;
    cnt = 0;
    for (int i = 1; i <= q; i++) {
      cin >> str[i];
    }
    for (int i = q; i >= 1; i--) {
      int len = str[i].size();
      for (int j = 0; j < len; j++) {
        ans[++cnt] = str[i][len - j - 1];
      }
      solve(len);
      int mm = maxn, id = 0;
      for (int j = 1; j <= len; j++) {
        if (mm > rak[j]) {
          mm = rak[j];
          id = j;
        }
      }
      cnt -= id - 1;
    }
    for (int i = cnt; i >= 1; i--) {
      cout <<(char)ans[i];
    }
    cout << endl;
  }
  return 0;
}
相关文章
|
存储 运维 监控
虚拟化技术-虚拟化概述|学习笔记
快速学习虚拟化技术-虚拟化概述
689 0
虚拟化技术-虚拟化概述|学习笔记
|
数据可视化 算法
【R语言实战】——kNN和朴素贝叶斯方法实战
【R语言实战】——kNN和朴素贝叶斯方法实战
java继承
Java中的抽象类是一种特殊的类,它可以包含抽象方法和普通方法。抽象方法是没有实现的方法,只有声明,没有实现。抽象类不能被实例化,只能被继承。继承抽象类的子类必须实现抽象类中的所有抽象方法,否则子类也必须声明为抽象类。抽象类可以有构造函数,但是不能是public或者protected,只能是private或者protected。抽象类可以有静态方法,也可以有非静态方法。抽象类可以有成员变量,也可以有静态成员变量。抽象类可以被继承,也可以被实现接口。
251 3
MyBatis-Plus---MyBatis-Plus代码生成器学习笔记
MyBatis-Plus---MyBatis-Plus代码生成器学习笔记
MyBatis-Plus---MyBatis-Plus代码生成器学习笔记
|
存储 运维 Kubernetes
云原生时代的DevOps平台设计之道
开发人员与运维人员是 IT 领域很重要的两大人群,他们都会参与到各种业务系统的建设过程中去
|
监控 Java 数据库连接
服务--systemd的管理
服务--systemd的管理
600 0
|
安全 前端开发 Java
Tomcat 爆出高危漏洞!
Tomcat 爆出高危漏洞!
630 0
Tomcat 爆出高危漏洞!
《OpenStack实战》——1.5 OpenStack发展历史
该法令签署120天后,美国宇航局(NASA)宣布它的开放政府框架,其中包括Nebula工具的共享。开发Nebula是为了加快向美国宇航局科学家和研究者提供IaaS资源的速度。与此同时,云计算提供商Rackspace宣布开源它的对象存储平台——Swift。
2925 0
|
大数据
大数据将终结收视率造假难题
  “我们最痛苦的莫过于,收视率原本是节目办的好不好的一项指标,台里也依此来考核我们,但收视率数据失真严重,节目办的再好,数据上体现不出来,最后全是白搭。”这是某省地方电视台一位栏目负责人向记者发出的心声。
1345 0
|
分布式计算 Hadoop Java
hadoop 集群常见错误解决办法
<p style="line-height:27.2px; color:rgb(51,51,51); font-family:Helvetica,Arial,sans-serif; font-size:14px"> <a target="_blank" rel="nofollow" class="ui-link" style="outline:none; color:rgb(36,137
3800 0