777D - Cloud of Hashtags

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 minimumpossible. 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.

题目大意:每个字符串删除一个后缀,删除掉的字母要尽量少,使得全部字符串成为一个非递减的字典序;
解题思路:逆着来,字符串删除后字典序一定是更小了,使用贪心策略,第n个不变,变第n-1个,然后第n-1个不变,变第n-2个,以此类推。
 
AC代码:
#include<stdio.h>
#include<iostream>
#include<vector>
#include<string>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<map>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
string s[500010];
int n, ans[500010];
int main() {
	int i, j, k;
	cin >> n;
	for (i = 1; i <= n; i++) {
		cin >> s[i];
	}
	ans[n] = s[n].length();
	for (i = n - 1; i >= 1; i--) {
		int l = s[i].length();
		bool f = false;
		for (j = 1; j < l&&j < ans[i+1]; j++) {
			if (s[i][j] < s[i + 1][j]) {
				ans[i] = l;
				f = true;
				break;
			}
			else if (s[i][j]>s[i+1][j]) {
				ans[i] = j;
				f = true;
				break;
			}
		}
		if (f==false) {
			ans[i] = min(l, ans[i + 1]);
		}
	}
	for (i = 1; i <= n; i++) {
		cout << '#';
		for (j = 1; j < ans[i]; j++) {
			cout << s[i][j];
		}
		cout << endl;
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值