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 ...
随机推荐
- 《MFC dialog中加入OpenGL窗体》
<MFC dialog中加入OpenGL窗体> 最近学习了如何在MFC对话框程序中加入OpenGL窗体的方法,在这里将自己的实现过程归纳一下. 步骤零: 加入PictureControl控 ...
- Vue-路由模式 hash 和 history
Vue 为了构建 SPA(单页面应用),需要引入前端路由系统,这也就是 Vue-Router 存在的意义.前端路由的核心,就在于 —— 改变视图的同时不会向后端发出请求. 创建的项目默认是hash模式 ...
- unittest---unittest错误截图
在做自动化的过程中,大多数执行者都不在旁边,那么如果用例失败了我们通常看报告上的失败信息,但是这样有时候可能不够清楚的判断到底哪里出了错误,我们还可以通过自动截图的功能,判断用例走到哪里出了错误. 截 ...
- 14. 深入解析Pod对象(一)
14. 深入解析Pod对象(一) """ 通过前面的讲解,大家应该都知道: Pod,而不是容器,它是 Kubernetes 项目中的最小编排单位.将这个设计落实到 API ...
- 关于null和空指针异常
1,null是一个标识符,用来表示不确定的对象,可以将null赋给引用类型变量,但不可以将null赋给基本类型变量 2,null本身不是对象,也不是object的实例,也不知道是什么类型 3,对于集合 ...
- AOP统一日志打印处理
在日常开发工作中,我们免不了要打印很多log.而大部分需要输出的log又是重复的(例如传入参数,返回值).因此,通过AOP方式来进行日志管理可以减少很多代码量,也更加优雅. Springboot通过A ...
- WLC-安装license
在CLI界面安装licenseStep 1 Install a license on the controller by entering this command:①license install ...
- MySQL优化2.索引
什么是索引: 索引的作用相当于图书的目录,可以根据目录中的页码快速找到所需的内容. 建立索引合适的列 经常用作where条件的列,order by排序的列 数据列不重复而且并不是唯一的几个值 不被经常 ...
- 疫情对国内5G发展的影响
导读 2020年春节期间,“新型冠状病毒”引发了自SARS之后又一次全国性疫情,而世卫组织也将之列为国际关注的突发公共卫生事件,随后多国发布了针对中国的旅行警告和入境限制,从当前情况来看,疫情对中国各 ...
- Codeforces Round #588 (Div. 2)E(DFS,思维,__gcd,树)
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;long long a[100007];vec ...