D. Cloud of Hashtags
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Vasya is an administrator of a public page of organization "Mouse and keyboard" and his everyday duty is to publish news from the world of competitive programming. For each news he also creates a list of hashtags to make searching for a particular topic more
comfortable. For the purpose of this problem we define hashtag as a string consisting of lowercase English letters and exactly one symbol '#' located at the
beginning of the string. The length of the hashtag is defined as the number of symbols in it without the symbol '#'.

The head administrator of the page told Vasya that hashtags should go in lexicographical order (take a look at the notes section for the definition).

Vasya is lazy so he doesn't want to actually change the order of hashtags in already published news. Instead, he decided to delete some suffixes (consecutive characters at the end of the string) of some of the hashtags. He is allowed to delete any number of
characters, even the whole string except for the symbol '#'. Vasya wants to pick such a way to delete suffixes that the total number of deleted symbols is minimum possible.
If there are several optimal solutions, he is fine with any of them.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 500 000) —
the number of hashtags being edited now.

Each of the next n lines contains exactly one hashtag of positive length.

It is guaranteed that the total length of all hashtags (i.e. the total length of the string except for characters '#') won't exceed 500 000.

Output

Print the resulting hashtags in any of the optimal solutions.

Examples
input
3
#book
#bigtown
#big
output
#b
#big
#big
input
3
#book
#cool
#cold
output
#book
#co
#cold
input
4
#car
#cart
#art
#at
output
#
#
#art
#at
input
3
#apple
#apple
#fruit
output
#apple
#apple
#fruit
Note

Word a1, a2, ..., am of
length m is lexicographically not greater than word b1, b2, ..., bk of
length k, if one of two conditions hold:

  • at first position i, such that ai ≠ bi,
    the character ai goes
    earlier in the alphabet than character bi,
    i.e. a has smaller character than bin
    the first position where they differ;
  • if there is no such position i and m ≤ k,
    i.e. the first word is a prefix of the second or two words are equal.

The sequence of words is said to be sorted in lexicographical order if each word (except the last one) is lexicographically not greater than the next word.

For the words consisting of lowercase English letters the lexicographical order coincides with the alphabet word order in the dictionary.

According to the above definition, if a hashtag consisting of one character '#' it is lexicographically not greater than any other valid hashtag. That's why
in the third sample we can't keep first two hashtags unchanged and shorten the other two.

——————————————————————————————————————

题意:给出几个字符串,要求删除尾部尽可能少的字母,是的单词按字典序上升(可以相等)

思路:从最后一个开始处理,每个单词尽可能少删除,可以开一个数组记录每个单词最多可以取到什么位置

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std; vector<char>s[500005];
char x[500005];
int ans[500005];
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++)
s[i].clear(); for(int i=0; i<n; i++)
{
scanf("%s",&x);
int k=strlen(x);
for(int j=0; j<k; j++)
s[i].push_back(x[j]);
}
ans[n-1]=s[n-1].size()-1;
for(int i=n-2; i>=0; i--)
{
int k=s[i].size();
for(int j=0; j<k; j++)
{
if((j==ans[i+1]||j==k-1)&&s[i][j]==s[i+1][j])
{
ans[i]=min(k-1,ans[i+1]);
break;
}
if(s[i][j]<s[i+1][j])
{
ans[i]=k-1;
break;
}
else if(s[i][j]>s[i+1][j])
{
ans[i]=j-1;
break;
}
}
} for(int i=0;i<n;i++)
{
for(int j=0;j<=ans[i];j++)
cout<<s[i][j];
cout<<endl;
} }
return 0;
}

Codeforces777D Cloud of Hashtags 2017-05-04 18:06 67人阅读 评论(0) 收藏的更多相关文章

  1. {A} + {B} 分类: HDU 2015-07-11 18:06 6人阅读 评论(0) 收藏

    {A} + {B} Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  2. POJ3045 Cow Acrobats 2017-05-11 18:06 31人阅读 评论(0) 收藏

    Cow Acrobats Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4998   Accepted: 1892 Desc ...

  3. HDU6023 Automatic Judge 2017-05-07 18:30 73人阅读 评论(0) 收藏

    Automatic Judge Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  4. Rebuild my Ubuntu 分类: ubuntu shell 2014-11-08 18:23 193人阅读 评论(0) 收藏

    全盘格式化,重装了Ubuntu和Windows,记录一下重新配置Ubuntu过程. //build-essential sudo apt-get install build-essential sud ...

  5. Doubles 分类: POJ 2015-06-12 18:24 11人阅读 评论(0) 收藏

    Doubles Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19954   Accepted: 11536 Descrip ...

  6. OpenCV与QT联合编译 分类: Eye_Detection ZedBoard OpenCV shell ubuntu 2014-11-08 18:54 143人阅读 评论(0) 收藏

    问题1:首先参考rainysky的博客,发现qmake时发生找不到目录,文件的错误,又找不到 qmake.conf 文件的写法.所以开始按照网上的程序修改 XXX.pro 文件. 问题2:使用QT C ...

  7. 移植QT到ZedBoard(制作运行库镜像) 交叉编译 分类: ubuntu shell ZedBoard OpenCV 2014-11-08 18:49 219人阅读 评论(0) 收藏

    制作运行库 由于ubuntu的Qt运行库在/usr/local/Trolltech/Qt-4.7.3/下,由makefile可以看到引用运行库是 INCPATH = -I/usr//mkspecs/d ...

  8. highgui.h备查 分类: C/C++ OpenCV 2014-11-08 18:11 292人阅读 评论(0) 收藏

    /*M/////////////////////////////////////////////////////////////////////////////////////// // // IMP ...

  9. OC基础:数组.字典.集 分类: ios学习 OC 2015-06-18 18:58 47人阅读 评论(0) 收藏

    ==============NSArray(不可变数组)=========== NSArray,继承自NSObject  用来管理(储存)一些有序的对象,不可变数组. 创建一个空数组 NSArray ...

随机推荐

  1. Delphi操作Excel(Use Oel)

    Use ComObj: procedure TorderMore1.BitBtn2Click(Sender: TObject);var  xlsFile:WideString;  var ExcelA ...

  2. time和datetime

    一.time模块常用函数1. time()函数time()函数返回的是时间戳(timestamp).所谓时间戳指的是从1970年1月1日00:00:00开始按秒计算的偏移量.其他返回时间戳方式的函数还 ...

  3. Python在pycharm中编程时应该注意的问题汇总

    1.缩进问题 在 pycharm 中点击 enter 自动进行了换行缩进,此时应该注意:比如 if   else  语句,后面跟着打印输出 print 的时候,一定注意是要if语句下的输出还是else ...

  4. kegg富集分析之:KEGGREST包(9大功能)

    这个包依赖极有可能是这个:https://www.kegg.jp/kegg/docs/keggapi.html ,如果可以看懂会很好理解 由于KEGG数据库分享数据的策略改变,因此KEGG.db包不在 ...

  5. byte,short,int,long数据之间的倍数关系

    基本数据类型 byte   =  -128和127------------------------------------------------------------2的8次方,1个字节 shor ...

  6. for循环计算阶乘的和,for循环计算阶乘倒数的和

    计算阶乘的和 //阶乘的和,5!+4!+3!+2! int a = 5; for(int b = 4; b > 0; b--) { a = a * b; } //先定义好最大数的阶乘是多少 in ...

  7. inner join和outer join

    内连接           只连接匹配的行 左外连接        包含左边表的全部行(不管右边的表中是否存在与它们匹配的行),以及右边表中全部匹配的行 A left join B等价于A left ...

  8. Android网络类型判断(2g、3g、wifi)

    判断网络类型是wifi,还是3G,还是2G网络,对不同 的网络进行不同的处理,现将判断方法整理给大家,以供参考   说明:下面用到的数据移动2G,联通2G,联通3G,wifi我都已经测试过,暂时手上 ...

  9. ROS Learning-008 beginner_Tutorials ROS话题

    ROS Indigo beginner_Tutorials-07 ROS话题 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubuntu 14.04.4 LT ...

  10. spring框架之AspectJ的XML方式完成AOP的开发

    1. 步骤一:创建JavaWEB项目,引入具体的开发的jar包 * 先引入Spring框架开发的基本开发包 * 再引入Spring框架的AOP的开发包 * spring的传统AOP的开发的包 * sp ...