Codeforces777D Cloud of Hashtags 2017-05-04 18:06 67人阅读 评论(0) 收藏
2 seconds
256 megabytes
standard input
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.
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.
Print the resulting hashtags in any of the optimal solutions.
3
#book
#bigtown
#big
#b
#big
#big
3
#book
#cool
#cold
#book
#co
#cold
4
#car
#cart
#art
#at
#
#
#art
#at
3
#apple
#apple
#fruit
#apple
#apple
#fruit
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) 收藏的更多相关文章
- {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 ...
- POJ3045 Cow Acrobats 2017-05-11 18:06 31人阅读 评论(0) 收藏
Cow Acrobats Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4998 Accepted: 1892 Desc ...
- 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 ...
- Rebuild my Ubuntu 分类: ubuntu shell 2014-11-08 18:23 193人阅读 评论(0) 收藏
全盘格式化,重装了Ubuntu和Windows,记录一下重新配置Ubuntu过程. //build-essential sudo apt-get install build-essential sud ...
- Doubles 分类: POJ 2015-06-12 18:24 11人阅读 评论(0) 收藏
Doubles Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19954 Accepted: 11536 Descrip ...
- 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 ...
- 移植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 ...
- highgui.h备查 分类: C/C++ OpenCV 2014-11-08 18:11 292人阅读 评论(0) 收藏
/*M/////////////////////////////////////////////////////////////////////////////////////// // // IMP ...
- OC基础:数组.字典.集 分类: ios学习 OC 2015-06-18 18:58 47人阅读 评论(0) 收藏
==============NSArray(不可变数组)=========== NSArray,继承自NSObject 用来管理(储存)一些有序的对象,不可变数组. 创建一个空数组 NSArray ...
随机推荐
- java编写一个汽车类,有属性:品牌、型号、排量、速度,有方法:启动、加速、转弯、刹车、息火
/* * 汽车实体类 * 类里面有属性和方法 */public class Car { String brand; //汽车品牌 String modelNumber; //汽车型号 ...
- scala-学习 1
目录 变量定义 scala定义两种变量: var 可变 初始化之后,可以多次被重新赋值 val 不可变 一旦被初始化 就不能再赋值. var firstarg :java.lang.String = ...
- javascript中this之说
this是在运行时基于函数的执行环境绑定的:在全局函数中,this等于window,而当函数被作为某个对象的方法调用时,this等于那个对象.不过,匿名函数的执行环境具有全局性,因此其this对象通常 ...
- SpringBoot配置文件YML 注意事项
YML读取注意事项 使用YML时遇到的坑: 最近在做项目时,遇到了一些在读取YML配置时发生的问题,在这里写一并写下来,希望给自己以及大家一个提示,能尽量避免在读取配置文件时发生这些错误,给开发带来不 ...
- linux 用户管理(3)----查看用户登录时间以及命令历史
1.查看当前登录用户信息 who命令: who缺省输出包括用户名.终端类型.登陆日期以及远程主机. who /var/log/wtmp 可以查看自从wtmp文件创建以来的每一次登陆情况 (1)-b:查 ...
- thymeleaf 拼接字符串与变量
参考https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html <span th:text="'The name o ...
- iperf——网络性能测试工具
一.前言 工作中遇到需要测试Linux服务器网卡占用率的场景,查阅资料后,发现iperf是一款合适的网络测速工具. 下面讲解一下如何使用iperf做网络性能测试. 二.基础知识 先补充一些基础知识: ...
- eclipse搭建struts2环境及所遇到的问题
最近几天一直在搭建struts2框架,本身struts2框架的搭建是非常简单的,但不知道为什么最近就是总是报错,报了一大串的错 首先就是每次在类的根路径下创建struts.xml时,就报错,也不知道为 ...
- OC -网络请求 - NSURLConnection - GET
#import "ViewController.h" @interface ViewController ()<NSURLConnectionDataDelegate> ...
- cin 不能直接读入空格,可以用getline(PAT统计字符数)
#include <iostream>#include <cstring>using namespace std; int main(){ string str; ...