Codeforces 777D:Cloud of Hashtags(暴力,水题)
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 \(a_1, a_2, ..., a_m\) of length \(m\) is lexicographically not greater than word \(b_1, b_2, ..., b_k\) of length \(k\), if one of two conditions hold:
- at first position \(i\), such that \(a_i ≠ b_i\), the character \(a_i\) goes earlier in the alphabet than character \(b_i\), i.e. \(a\) has smaller character than \(b\) in 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.
题意
\(n\)个字符串,要求不改变位置,删除最少的字符串的后缀,使这些字符串按照字典序非递减的顺序排列
思路
暴力。
从后往前比较相邻的两个字符串的字典序,如果第\(i-1\)个字符串字典序小于第\(i\)个,那么就不需要删。否则,找到第\(i-1\)个字符串到哪个位置为止,可以满足字典序不大于第\(i\)个的字典序,然后截取出来这段字符串即可
代码
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=1e6+10;
const int mod=1e9+7;
const int maxm=1e3+10;
using namespace std;
vector<string>ve;
vector<string>ans;
int get_place(string s1,string s2)
{
int l1=s1.length();
int l2=s2.length();
int i;
for(i=0;i<min(l2,l1);i++)
{
if(s1[i]<s2[i])
return l1;
if(s1[i]==s2[i])
continue;
if(s1[i]>s2[i])
return i;
}
if(i==l2)
{
if(l1>l2)
{
if(s1[i-1]==s2[i-1])
return l2;
else
return l1;
}
}
return l1;
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
srand((unsigned int)time(NULL));
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin>>n;
string s;
for(int i=0;i<n;i++)
{
cin>>s;
ve.push_back(s);
}
string s1,s2;
ans.push_back(ve[n-1]);
for(int i=n-2;i>=0;i--)
{
s1=ve[i];
s2=ans[n-(i+2)];
int pos=get_place(s1,s2);
string ss;
ss=s1.substr(0,pos);
ans.push_back(ss);
}
for(int i=n-1;i>0;i--)
cout<<ans[i]<<endl;
cout<<ve[n-1]<<endl;
#ifndef ONLINE_JUDGE
cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl;
#endif
return 0;
}
Codeforces 777D:Cloud of Hashtags(暴力,水题)的更多相关文章
- Codeforces 777D Cloud of Hashtags(贪心)
题目链接 Cloud of Hashtags 题目还是比较简单的,直接贪心,但是因为我有两个细节没注意,所以FST了: 1.用了cin读入,但是没有加 std::ios::sync_with_stdi ...
- Codeforces 777D:Cloud of Hashtags(水题)
http://codeforces.com/problemset/problem/777/D 题意:给出n道字符串,删除最少的字符使得s[i] <= s[i+1]. 思路:感觉比C水好多啊,大概 ...
- Educational Codeforces Round 7 B. The Time 水题
B. The Time 题目连接: http://www.codeforces.com/contest/622/problem/B Description You are given the curr ...
- Educational Codeforces Round 7 A. Infinite Sequence 水题
A. Infinite Sequence 题目连接: http://www.codeforces.com/contest/622/problem/A Description Consider the ...
- Codeforces Testing Round #12 A. Divisibility 水题
A. Divisibility Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/probl ...
- [Usaco2008 Feb]Line连线游戏[暴力][水题]
Description Farmer John最近发明了一个游戏,来考验自命不凡的贝茜.游戏开始的时 候,FJ会给贝茜一块画着N (2 <= N <= 200)个不重合的点的木板,其中第i ...
- Codeforces Beta Round #37 A. Towers 水题
A. Towers 题目连接: http://www.codeforces.com/contest/37/problem/A Description Little Vasya has received ...
- codeforces 677A A. Vanya and Fence(水题)
题目链接: A. Vanya and Fence time limit per test 1 second memory limit per test 256 megabytes input stan ...
- CodeForces 690C1 Brain Network (easy) (水题,判断树)
题意:给定 n 条边,判断是不是树. 析:水题,判断是不是树,首先是有没有环,这个可以用并查集来判断,然后就是边数等于顶点数减1. 代码如下: #include <bits/stdc++.h&g ...
- Codeforces - 1194B - Yet Another Crosses Problem - 水题
https://codeforc.es/contest/1194/problem/B 好像也没什么思维,就是一个水题,不过蛮有趣的.意思是找缺黑色最少的行列十字.用O(n)的空间预处理掉一维,然后用O ...
随机推荐
- 零基础学习java------35---------删除一个商品案例,删除多个商品,编辑(修改商品信息),校验用户名是否已经注册(ajax)
一. 删除一个商品案例 将要操作的表格 思路图 前端代码 <%@ page language="java" contentType="text/html; cha ...
- Linux学习 - 输入输出重定向,管道符,通配符
一.键盘输入读取read read [选项] [变量名] -p [显示信息] 在等待read输入时,输出提示信息 -t [秒数] 指定read输入等待时间 -n [字符数] 指定read只接收n个字符 ...
- 如何用shell脚本分析网站日志统计PV、404、500等数据
以下shell脚本能统计出网站的总访问量,以及404,500出现的次数.统计出来后,可以结合监控宝来进行记录,进而可以看出网站访问量是否异常,是否存在攻击.还可以根据查看500出现的次数,进而判断网站 ...
- Activiti工作流引擎使用详解(一)
一.IDEA安装activiti插件 在插件库中查找actiBPM,安装该插件,如果找不到该插件,请到插件库中下载该包手动安装,插件地址 http://plugins.jetbrains.com/pl ...
- System.exit(-1)和return 的区别
对于只有一个单一方法的类或者系统来说是一样的,但是对于含有多个类和方法,且调用关系比较复杂时就不一样了. System.exit(-1)是指所有程序(方法,类等)停止,系统停止运行. return只是 ...
- JS21. 使用原生JS封装一个公共的Alert插件(HTML5: Shadow Dom)
效果预览 Shadow DOM Web components 的一个重要属性是封装--可以将标记结构.样式和行为隐藏起来,并与页面上的其他代码相隔离,保证不同的部分不会混在一起,可使代码更加干净.整 ...
- 【简】题解 AWSL090429 【价值】
先考虑当要选的物品一定时 显然有个贪心 wi越小的要越先选 所以先按wi从小到大拍序 因为发现正着递推要记录的状态很多 并且wi的贡献与后面选了几个物品有关 考虑正难则反 倒着递推 提前计算wi的贡献 ...
- Moment.js使用笔记
零.前情提要 上个月开发了数据平台,用的框架是vue + Ant Design of Vue,其中用了组件[range-picker]日期选择框,涉及到时间方法就去看了momentJS,以此记录~ 如 ...
- 20 个 .NET 6 新增的 API
DateOnly & TimeOnly .NET 6 引入了两种期待已久的类型 - DateOnly 和 TimeOnly, 它们分别代表DateTime的日期和时间部分. DateOnly ...
- Mysql资料 xtrabackup
目录 一.简介 原理 优缺点 二.安装 三.日常使用 备份所有库 增量备份 远程备份 四.参数 一.简介 原理 其实XtraBackup也是基于INNODB的 crash-recovery功能来实现的 ...