D. Cloud of Hashtags

52 篇文章 1 订阅

https://codeforces.com/problemset/problem/777/D

题目描述

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 nn ( 1<=n<=5000001<=n<=500000 ) — the number of hashtags being edited now.

Each of the next nn 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 500000500000 .

输出格式

Print the resulting hashtags in any of the optimal solutions.

题意翻译

给出nn个开头是'#'的字符串,你需要把这些字符串按字典序从小到大排列.
你可以把某一个字符串的任意后缀去掉(当然你甚至可以去掉整个字符串),要求你去掉的字符数量最少.
输出你操作完的这nn个字符串.

输入输出样例

输入 #1复制

3
#book
#bigtown
#big

输出 #1复制

#b
#big
#big

输入 #2复制

3
#book
#cool
#cold

输出 #2复制

#book
#co
#cold

输入 #3复制

4
#car
#cart
#art
#at

输出 #3复制

#
#
#art
#at

输入 #4复制

3
#apple
#apple
#fruit

输出 #4复制

#apple
#apple
#fruit

说明/提示

Word a_{1},a_{2},...,a_{m}a1​,a2​,...,am​ of length mm is lexicographically not greater than word b_{1},b_{2},...,b_{k}b1​,b2​,...,bk​ of length kk , if one of two conditions hold:

  • at first position ii , such that a_{i}≠b_{i}ai​​=bi​ , the character a_{i}ai​ goes earlier in the alphabet than character b_{i}bi​ , i.e. aa has smaller character than bb in the first position where they differ;
  • if there is no such position ii and m<=km<=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.


题意:这些字符串按字典序从小到大排列.

思路:考虑到所有字母总长度<=5e5,O(n)模拟

因为最后一个字符串如果删字符的话,最后一个字符串的字典序只会变小,所以最后一个字符串是没必要删去的。

那么从后往前思考,如果上面的字符串当前位置>下面字符串当前位置,那么上面字符串当前位置以及后面是都要删去的。

如果上面字符串当前位置<下面字符串当前位置,那么就满足字典序从小到大,为了使删除的总字符数目最少,贪心来说就没必要删去了。

按照这个思路模拟就好了。

反向思考的题。

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=5e5+100;
typedef long long LL;
string s[maxn];
LL n;
void check(string &a,string &b)
{
	if(b=="#")
	{
		a="#";return;
	}
	for(LL i=0;i<b.size();i++)
	{
		if(b[i]<a[i]) break; 
		if(b[i]>a[i])
		{
		   LL len=b.length()-i;
		   b.erase(i,len);	
		}	
	}	
}
void solve()
{
	for(LL i=n;i>=1;i--)
	{
		check(s[i],s[i-1]);
	}
	for(LL i=1;i<=n;i++) cout<<s[i]<<endl; 
}
int main(void)
{
  cin>>n;
  for(LL i=1;i<=n;i++) cin>>s[i];
  solve();
return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值