【codeforces 777D】Cloud of Hashtags
【题目链接】:http://codeforces.com/contest/777/problem/D
【题意】
让你通过删除字符串的末尾字符的方式;
使得所给的字符串以字典序升序排序;
不能交换字符串之间的位置;
【题解】
/*
要求字符串的字典序升序排;
不能交换字符串的位置;
那就使得
s[i]<=s[i+1]对i∈[1..n-1]都成立就好;
(这里的<=指的是字典序的小于等于);
从最后一个字符串开始;
逆序处理字符串1..n-1;
对比字符串s[i]和s[i+1]的所有字符;
对比的时候len = min(s[i].size(),s[i+1].size());
找到第一个字符不一样的位置pos;
如果找到了这样一个pos;
记录它
if (s[i][pos]<s[i+1][pos])
则这个字符串出现在这里是合法的;
符合s[i]<=s[i+1]
if (s[i][pos]>s[i+1][pos])
则把s[i]从pos开始全部删掉;
然后处理下一个;
当然,如果pos没有找到的话,就比较s[i].size()和s[i+1].size();
如果s[i].size()<=s[i+1].size();则合法
否则从s[i].size()+1开始把s[i]从s[i].size()+1一直到结束全部删掉
(可以不用真的删掉吧。写一个to[n];
表示每个字符串输出的时候要一直输出到哪个地方.
*/
【完整代码】
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 5e5 + 100;
string s[N];
int n;
int to[N];
bool bo[N];
int main()
{
//读入优化??
//freopen("D:\\rush.txt", "r", stdin);
rei(n);
rep1(i, 1, n)
cin >> s[i];
int len = s[n].size();
len--;
to[n] = len;
rep2(i, n - 1, 1)
{
int len = min(to[i + 1], int(s[i].size())-1);
int pos = -1;
rep1(j,1,len)
if (s[i][j] != s[i + 1][j])
{
pos = j;
break;
}
if (pos != -1)
{
if (s[i][pos] < s[i + 1][pos])
{
to[i] = int(s[i].size())-1;
continue;
}
if (s[i][pos] > s[i + 1][pos])
{
to[i] = pos - 1;
continue;
}
}
if (int(s[i].size()) <= to[i + 1])
{
to[i] = int(s[i].size())-1;
continue;
}
to[i] = to[i + 1];
}
rep1(i, 1, n)
{
putchar('#');
rep1(j, 1, to[i])
putchar(s[i][j]);
puts("");
}
return 0;
}
【codeforces 777D】Cloud of Hashtags的更多相关文章
- Codeforces 777D:Cloud of Hashtags(水题)
http://codeforces.com/problemset/problem/777/D 题意:给出n道字符串,删除最少的字符使得s[i] <= s[i+1]. 思路:感觉比C水好多啊,大概 ...
- Codeforces 777D:Cloud of Hashtags(暴力,水题)
Vasya is an administrator of a public page of organization "Mouse and keyboard" and his ev ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
随机推荐
- Something-Summary
1.Combinatorial Mathematics 1.1 Bell Number: \(B_n\)表示元素个数为n的集合划分成若干个不相交集合的方案数. \(B_{n + 1} = \sum_{ ...
- GCJ 2008 Round 1A Minimum Scalar Product
https://code.google.com/codejam/contest/32016/dashboard 题目大意: GCJ(google code jam)上的水题.下周二有比赛,来熟悉熟悉. ...
- 学习C#修饰符:类修饰符和成员修饰符
C#修饰符之类修饰符:public.internal. partial.abstract.sealed.static C#修饰符之成员修饰符:public.protected.private.inte ...
- element ui源码解析 -- input篇
el-input是element ui中使用最频繁的组件之一了,分析其构成从四个方面入手:DOM结构,属性,样式,事件入手 DOM结构: <div> <input /> < ...
- [WASM] Call a JavaScript Function from WebAssembly
Using WASM Fiddle, we show how to write a simple number logger function that calls a consoleLog func ...
- CSDN日报20170406 ——《代码非常烂,所以离职。》
[程序人生]代码非常烂.所以离职? 作者:stormzhang 我在面试的时候一般会问这么一个问题:你为什么离职? 当中有不少同学会提到这么一个原因.现在的项目代码太烂了,前人留下了非常多坑,我实在忍 ...
- Java反射学习总结一(基础篇)
Class类是Reflection API中核心的类,他位于Java.lang.Class 列出一些常用的方法. - getName() : 获得类的完整名字 - getFields() : 获得类的 ...
- occcont.cpp 925
将InitInstance()中调用的 InitATL() 替换为 AfxOleInit()即可.
- [Angular] Alternative Themes - Learn the Host-Context Selector
To add alernative theme, we can use :host-context() selector from Angular. //au-fa-input-red-theme.c ...
- 【2005】N只猴子选大王
Time Limit: 3 second Memory Limit: 2 MB N只猴子选大王.选举办法如下:从头到尾1.2.3报数,凡报3的退出,余下的从尾到头1.2.3报数,凡报3退出:余下的又从 ...