【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) = ...
随机推荐
- 最小二乘法,python3实现
https://www.cnblogs.com/BlogOfMr-Leo/p/8627311.html [用的是库函数] https://blog.csdn. ...
- 修改shm,oracle11g需要扩大共享内存
作者:david_zhang@sh [转载时请以超链接形式标明文章] 链接:http://www.cnblogs.com/david-zhang-index/archive/2012/04/26/24 ...
- 洛谷 P2693 [USACO1.3]号码锁 Combination Lock
P2693 [USACO1.3]号码锁 Combination Lock 题目描述 农夫约翰的奶牛不停地从他的农场中逃出来,导致了很多损害.为了防止它们再逃出来,他买了一只很大的号码锁以防止奶牛们打开 ...
- [React] Remove React PropTypes by using Flow Annotations (in CRA)
Starting from v15.5 if we wanted to use React's PropTypes we had to change our code to use a separat ...
- Android 文件路径(/mnt/sdcard/...)、Uri(content://media/external/...)
一.URI 通用资源标志符(Universal Resource Identifier, 简称"URI"). Uri代表要操作的数据,Android上可用的每种资源 - 图像.视频 ...
- Altium Designer如何统一改变pcb状态下的原件标号位置
原创 我用的是Altium Designer16版本 变成 步骤如下: 选中标号 右击 下边一步很重要: 点击应用和确定 在之后弹出的对话框中选则你要改变的位置,我这里是把标号改变到原件的右侧: 等待 ...
- CentOS下利用Docker部署Surging
原文:CentOS下利用Docker部署Surging 1. 安装Centos, 配置固定ip配置文件地址vi /etc/sysconfig/network-scripts/ifcfg-ens33`` ...
- addSubview 与 removeFromSuperview
//当前视图的父视图添加和本视图同级的视图 [self.view.superview addSubview:showview.view]; //从父视图移除当前视图 [self.view remove ...
- [Algorithms] Binary Search Algorithm using TypeScript
(binary search trees) which form the basis of modern databases and immutable data structures. Binary ...
- 结合Wireshark捕获分组深入理解TCP/IP协议之以太网帧
摘要: 本文摘抄并整理了以太网相关理论知识,包括CSMA/CD协议机制及工作.LAN互连,详细分析了Ethernet II帧格式,最后给出Ethernet II帧实例. 一.以太网[1] 1. ...