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. How to Pronounce ‘to the’ in a Sentence

    How to Pronounce ‘to the’ in a Sentence Share Tweet Share Tagged With: The Word THE, TO Reduction St ...

  2. python中时间差中seconds和total_seconds

    在python中经常会用到计算两个时间差,两个日期类型进行相减可以获取到时间差 经常会使用seconds来获取,其实seconds获取的是时间差的秒数,遗漏了天 seconds是获取时间部分的差值,而 ...

  3. 在Eclipes中查看源代码和大纲快速定位

    1 在Eclipes中查看源代码,快捷键使用clrl+光标,选择你要查看的方法和属性查看源代码.例如你想看StringBuilder这个类源代码 StringBuilder allow = new S ...

  4. MySQL中实现Oracle里面 rank()over ( PARTITION BY ORDER BY) 分类分组功能

    各班级学生成绩测试表 select * from TMP_A; 实现目的: 按照班级分类后按照分数倒序排序 采用MySQL变量简单实现,SQL如下: SELECT  a.stu_id,a.point, ...

  5. 在Ubuntu上安装微信

    1) 从https://github.com/geeeeeeeeek/electronic-wechat/releases地址中下载linux-x64.tar.gz文件到/opt/wechat文件夹 ...

  6. 大型运输行业实战_day03_2_使用ajax将请求页面与请求数据分离

    1.引入jquery 1.添加jquery包 2.在要使用jquery的页面中引入jquery 引入jquery后必须检查是否引入正确,这里值得注意的是 springMVC默认情况先会拦截 js文件, ...

  7. JSP页面与JSP页面之间传输参数出现中文乱码的解决方案

    在学习编程初期JSP与JSP页面之间传输参数大多数都是使用这样的方式 index.jsp?id=*&name=* 这样的传输方式实质上是一种GET传输方式, 那如果出现了中文乱码, 解决方法其 ...

  8. OpenCV(图像处理)—访问像素的三种方法

    方法一:用指针访问像素 #include <opencv2/opencv.hpp> #include <opencv2/core/core.hpp> #include < ...

  9. ssh登录忽略known_hosts列表

    编辑 ~/.ssh/config 如果不存在,可以先创建,并且要注意其读写权限 mkdir -p ~/.ssh touch ~/.ssh/config ~/.ssh/config 文件内容如下 Log ...

  10. Ruby环境配置

    一.rvm 1.简介 rvm是一个命令行工具,可以提供一个便捷的多版本ruby环境的管理和切换. 2.安装步骤 1.新建文件:rvm-installer.sh vi rvm-installer.sh ...