【贪心】Codeforces Round #401 (Div. 2) D. Cloud of Hashtags

从后向前枚举字符串,然后从左向右枚举位。

如果该串的某位比之前的串的该位小,那么将之前的那串截断。

如果该串的某位比之前的串的该位大,那么之前那串可以直接保留全长度。

具体看代码。

#include<cstdio>
#include<iostream>
#include<string>
using namespace std;
string a[500010];
int n,lens[500010],b[500010];
int main()
{
//	freopen("d.in","r",stdin);
	scanf("%d",&n);
	for(int i=1;i<=n;++i)
	  {
	  	cin>>a[i];
	  	b[i]=a[i].length();
	  }
	lens[n]=b[n];
	for(int i=n;i>1;--i)
	  {
	  	bool flag=0;
	  	for(int j=0;j<lens[i];++j)
	  	  {
	  	  	if(j>=b[i-1])
	  	  	  {
	  	  	  	lens[i-1]=b[i-1];
	  	  	  	flag=1;
	  	  	  	break;
	  	  	  }
	  	    if(a[i][j]<a[i-1][j])
	  	      {
	  	      	lens[i-1]=j;
	  	      	flag=1;
	  	      	break;
	  	      }
	  	    if(a[i][j]>a[i-1][j])
	  	      {
	  	      	lens[i-1]=b[i-1];
	  	      	flag=1;
	  	      	break;
	  	      }
	  	  }
	  	if(!flag)
	  	  lens[i-1]=lens[i];
	  }
	for(int i=1;i<=n;++i)
	  {
	  	for(int j=0;j<lens[i];++j)
	  	  putchar(a[i][j]);
	  	puts("");
	  }
	return 0;
}
时间: 2024-08-07 20:54:37

【贪心】Codeforces Round #401 (Div. 2) D. Cloud of Hashtags的相关文章

Codeforces Round #401 (Div. 2) D. Cloud of Hashtags

题目链接:D. Cloud of Hashtags 题意: 给你n个字符串,让你删后缀,使得这些字符串按字典序排列,要求是删除的后缀最少 题解: 由于n比较大,我们可以将全部的字符串存在一个数组里面,然后记录一下每个字符串的开始位置和长度,然后从下面往上对比. 如果str[i][j]<str[i+1][j],直接退出,因为这里已经成为字典序. 如果str[i][j]>str[i+1][j],那么就把str[i][j]=0,表示从这里开始的后缀全部删掉. str[i][j]==str[i+1][

Codeforces Round #401 (Div. 2) E 贪心,线段树

Codeforces Round #401 (Div. 2) A 循环节 B 暴力排一下 C 标记出来,但10^5,特耿直地码了个O(n^2)的上去,最气的是在最后3分钟的时候被叉== D 从后往前贪心暴糙一下就好.比赛时一眼瞄出来了不敢写,搞不懂这样竟然不会超时.. E. Hanoi Factory 题意:n个环体,内径a[i],外径b[i],高h[i].当 a[i+1]<b[i]<=b[i+1] 时,第 i 个环体可以堆在第 i+1个环体上.求可以堆出的最高高度. tags:佩服那些大佬,

Codeforces Round #401 (Div. 2) E. Hanoi Factory

题目链接:Codeforces Round #401 (Div. 2) E. Hanoi Factory 题意: 给你n个环,每个环有内径a,外径b,高度v,现在让你将这n个环重起来,问你能重的最大高度. 满足条件:bi>=bj,bj>ai.(i<j) 题解: 首先将所以数据离散化,然后我们先按b从大到小排序,以a为第二关键字. 这里有一个贪心的思想,就是b相同时,a越小的放上面,这样可以重更多. 然后用树状数组维护一下放当前环的最大值,更新一下答案就行. 这题也可以直接cdq分治,不过

贪心 Codeforces Round #303 (Div. 2) B. Equidistant String

题目传送门 1 /* 2 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 3 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 #include <iostream> 10 using namespace std; 11 12 const int MAX

贪心 Codeforces Round #191 (Div. 2) A. Flipping Game

题目传送门 1 /* 2 贪心:暴力贪心水水 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 using namespace std; 8 9 const int MAXN = 1e2 + 10; 10 const int INF = 0x3f3f3f3f; 11 int a[MAXN]; 12 13 int main(void) //Codeforces Round #191

贪心 Codeforces Round #301 (Div. 2) B. School Marks

题目传送门 1 /* 2 贪心:首先要注意,y是中位数的要求:先把其他的都设置为1,那么最多有(n-1)/2个比y小的,cnt记录比y小的个数 3 num1是输出的1的个数,numy是除此之外的数都为y,此时的numy是最少需要的,这样才可能中位数大于等于y 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <algorithm> 8 #include <cstring> 9 using na

贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks

题目传送门 1 /* 2 题意:给n个棍子,组成的矩形面积和最大,每根棍子可以-1 3 贪心:排序后,相邻的进行比较,若可以读入x[p++],然后两两相乘相加就可以了 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 using namespace std; 10 11 typedef long long ll; 12 13 co

贪心 Codeforces Round #135 (Div. 2) C. Color Stripe

题目传送门 1 /* 2 贪心:当m == 2时,结果肯定是ABABAB或BABABA,取最小改变量:当m > 2时,当与前一个相等时, 改变一个字母 3 同时不和下一个相等就是最优的解法 4 */ 5 #include <cstdio> 6 #include <cstring> 7 #include <algorithm> 8 using namespace std; 9 10 const int MAXN = 5e5 + 10; 11 const int IN

字符串处理/贪心 Codeforces Round #307 (Div. 2) B. ZgukistringZ

题目传送门 1 /* 2 题意:任意排列第一个字符串,使得有最多的不覆盖a/b字符串出现 3 字符串处理/贪心:暴力找到最大能不覆盖的a字符串,然后在b字符串中动态得出最优解 4 恶心死我了,我最初想输出最多的a,再最多的b,然而并不能保证是最多的:( 5 */ 6 #include <cstdio> 7 #include <cstring> 8 #include <string> 9 #include <iostream> 10 #include <