2017 青岛现场赛 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 = suf1suf_1suf1 + suf2suf_2suf2 + · · · + sufnsuf_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 s1s_1s1 , s2s_2s2 , · · · , sns_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 题意:给n个字符串,从每个字符串种选择一个最小后缀,要求把每个字符串的最小后缀拼接在一起组成一个字典序最小的字符串并输出 题解:暴力,从最后一个字符串开始处理,用for找到每个字符串的最小后缀,再用s.substr()函数拼接后缀,输出结果 注意:如果每次找到一个字符串的最小后缀,然后把所用后缀拼接起来可能并不是最终答案
比如输入2个字符串
aabaa
cc
第一个字符串的最小后缀是aa
第二个最小后缀是c
如果简单拼接,答案是aac
但真正结果是aabaac
因为后面的答案会影响前面的后缀串大小,因此我们得先求出最后的最小后缀串,然后将其加到上一个串的结尾,再求前面的串的最小的后缀串,以此类推再上一个串即可
题目来源
/*
string s;
s.substr(start,len);//字符串提取函数,从位置start开始提取长度为len的子串
*/ #include<iostream>
#include<stdio.h>
#include<string>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N=;
string str[];
int anslen,nowlen;
int len[N];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
str[]="";
int n;
scanf("%d",&n);
for(int i=; i<=n; i++)
{
cin>>str[i];
len[i]=str[i].size();
} anslen=;
nowlen=; for(int i=n; i>=; i--)//从最后一个字符串开始处理
{
int pos=;
nowlen=str[i].size();
for(int j=; j<=len[i]; j++)//j<=len[i]保证处理每一个字符串的时候都会取一个后缀
{
if(str[i].substr(pos-,nowlen-pos+)>str[i].substr(j-,nowlen-j+) )//找到后缀最小的字符串开始的位置pos
{
pos=j;
}
}
str[i-]+=str[i].substr(pos-,nowlen-pos+);//将第i个字符串的最小后缀拼接到第i-1个字符串上
}
cout<<str[]<<endl; } return ;
}
2017 青岛现场赛 Suffix的更多相关文章
- 2017 青岛现场赛 I The Squared Mosquito Coil
Lusrica designs a mosquito coil in a board with n × n grids. The mosquito coil is a series of consec ...
- 2018ICPC青岛现场赛 重现训练
先贴代码,以及简要题解. 和一个队友下午双排打了一下,队友光速签到,我签的J被嫌弃写得慢以及演员...然后我秒出了E了思路然而难以置信这么简单的思路当时才过了十几个,于是发现D.F不是太好做.最后交了 ...
- HDU 6212 Zuma 2017青岛网络赛 区间DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6212 解法:看了眼题就发现这个BZOJ 1032不是一毛一样?但是BZOJ上那是个巨坑,数据有错,原来 ...
- 2018 ACM-ICPC青岛现场赛 B题 Kawa Exam 题解 ZOJ 4059
题意:BaoBao正在进行在线考试(都是选择题),每个题都有唯一的一个正确答案,但是考试系统有m个bug(就是有m个限制),每个bug表示为第u个问题和第v个问题你必须选择相同的选项,题目问你,如果你 ...
- 2018ACM/ICPC 青岛现场赛 E题 Plants vs. Zombies
题意: 你的房子在0点,1,2,3,...,n(n<=1e5)点每个点都有一颗高度为0的花,浇一次水花会长a[i]. 你有一个机器人刚开始在你家,最多走m步,每一步只能往前走或者往后走,每走到一 ...
- 2018 ACM-ICPC 亚洲区域赛青岛现场赛 —— Problem F. Tournament
题面:http://acm.zju.edu.cn/contest-materials/qd2018/qd2018_problems.pdf 题意: n个骑士决斗K轮 要求是每个骑士只能跟另外一个骑士决 ...
- 2017南宁现场赛E The Champion
Bob is attending a chess competition. Now the competition is in the knockout phase. There are 2^r2r ...
- 2017青岛网络赛1011 A Cubic number and A Cubic Number
A Cubic number and A Cubic Number Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65535/3276 ...
- 2017青岛网络赛1008 Chinese Zodiac
Chinese Zodiac Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) T ...
随机推荐
- Tensorflow机器学习入门——读取数据
TensorFlow 中可以通过三种方式读取数据: 一.通过feed_dict传递数据: input1 = tf.placeholder(tf.float32) input2 = tf.placeho ...
- 3_5 生成元(UVa1583)
如果x加上x的各个数字之和得到y,就说x是y的生成元.给出n(1<=n<=100000),求最小生成元.无解输出0.例如,n=216,121,2005时的解分别为198,0,1979. 样 ...
- Dism++ 更新管理提示“无法连接服务器”
Dism++ 更新管理提示"无法连接服务器" 下载wsusscn3.cab,放入Dism++安装目录下Config文件夹中.
- Unity中调用Windows窗口选择文件
1.OpenFileName数据接收类,如下: using UnityEngine; using System.Collections; using System; using System.Runt ...
- git 操作详情
Git 教程 1.Git 是一个开源的分布式版本控制系统 2.Git 是 Linux Torvalds 为了帮助管理 Linux 内核开发而开发的一个开源版本控制软件 3.Git 与常用的版本控制工具 ...
- Mac 系统上有趣的插件
1.微信小助手:https://github.com/TKkk-iOSer/WeChatPlugin-MacOS 作用:开启消息撤回拦截,设置自动回复,远程登录Mac,微信多开,免认证登录.... 2 ...
- Python编程使用PyQT制作视频播放器
最近研究了Python的两个GUI包,Tkinter和PyQT.这两个GUI包的底层分别是Tcl/Tk和QT.相比之下,我觉得PyQT使用起来更加方便,功能也相对丰富.这一篇用PyQT实现一个视频播放 ...
- PAT T1021 Safe Fruit
暴力搜索加剪枝~ #include<bits/stdc++.h> using namespace std; ; const int inf=1e9; int g[maxn][maxn]; ...
- 十三 Struts2复杂类型的数据封装,List封装和Map封装
在实际开发当中,有可能遇到批量向数据库中插入记录,需要在页面中将数据封装到集合中.类似页面表达式方法 List封装: 前端JSP: <%@ page language="java&qu ...
- VM安装linux操作系统详细教程
1.首先我们新建一个虚拟机,先不安装操作系统,稍后再对其安装Linux系统. 新建虚拟机步骤如下: 打开VMware软件,菜单栏点击“文件(F)”–>选择“新建虚拟机(N)”,如下图1,(或者直 ...