【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) = ...
随机推荐
- php网站修改为https后curl报错301
今日测试项目时需调用post模拟传参测试接口是否可用,但返回报错信息(301永久迁移),在网上搜寻解决办法无果,最后发现只要将跳转地址修改为https://+url的形式就可以了
- 【Codeforces Round #447 (Div. 2) B】Ralph And His Magic Field
| [链接] 我是链接,点我呀:) [题意] 给你一个n*m矩阵,让你在里面填数字. 使得每一行的数字的乘积都为k; 且每一列的数字的乘积都为k; k只能为1或-1 [题解] 显然每个位置只能填1或- ...
- 开发过程使用Tomcat Maven插件持续快捷部署Web项目
我在平时工作中部署Web项目到测试服务器上的Tomcat时用的是Hudson.Hudson本身已经跟SVN.Git.Maven集成并且支持添加各种插件.但如果使用Hudson,我需要配置两个任务:一个 ...
- delete noprompt archivelog 报错ORA-00245,RMAN-08132
在RMAN执行 delete noprompt archivelog until time 'sysdate-1'; 报错 ORA-00245: control file backup fai ...
- SN74HC573ANSR 锁存器
这是它的工作电压范围 这个是功能表,数电上曾经学过: 内部的框图,也是数电的知识:
- 微服务实践(七):从单体式架构迁移到微服务架构 - DockOne.io
原文:微服务实践(七):从单体式架构迁移到微服务架构 - DockOne.io [编者的话]这是用微服务开发应用系列博客的第七篇也是最后一篇.第一篇中介绍了微服务架构模式,并且讨论了微服架构的优缺点: ...
- leetcode——Reverse Linked List II 选择链表中部分节点逆序(AC)
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1-> ...
- zoj 2724 Windows Message Queue 优先队列
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1724 题目大意: 给出两种操作,GET要求取出当前队首的元素,而PUT会输入名 ...
- POJ 2014 Flow Layout 模拟
http://poj.org/problem?id=2014 嘻嘻2014要到啦,于是去做Prob.ID 为2014的题~~~~祝大家新年快乐~~ 题目大意: 给你一个最大宽度的矩形,要求把小矩形排放 ...
- 6、USB Video Class Specification
关于USB Class 将设备归为既定的类别,并对相应类别的设备的在USB协议的应用级协议和接口作出规范,这样只要按照类设备的标准实现驱动程序和设备,则一套驱动可以驱动这一类的所有设备,而这一类设备可 ...