Codeforces909D Colorful Points(缩点)
http://codeforces.com/problemset/problem/909/D
直接模拟超时。要运用缩点的方法,把相同的一段缩成一点,记录有几个。
对于非首尾的缩点每次-2,首尾的-1。
注意strlen不要放循环里,因为这个超时找了好久。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define INF 0x3f3f3f3f
#define MAXN 500010
const int MOD=1e9+;
typedef long long ll;
using namespace std;
typedef struct{
char c;
int num;
}Node;
Node node[];
char ss[];
int len;
int update(int len)
{
int j=, flag = ;
for(int i = ; i < len; i++){
if(node[i].num>){//该点还有几个
if(!flag){//第一次找到,直接赋给node[0]
flag = ;
node[j] = node[i];
}
else if(node[i].c == node[j].c){
node[j].num += node[i].num;
}
else{
node[++j] = node[i];
}
}
}
return j;
}
int solve()
{
int len = update(strlen(ss))+, cnt=;//第一次update用的是缩点前长度,得到缩点后长度
while(len>){
for(int i = ;i < len-; i++){
node[i].num -= ;
}
node[].num--; node[len-].num--;
len = update(len)+;
cnt++;
}
return cnt;
}
int main()
{
IO;
cin >> ss;
int len = strlen(ss);
for(int i = ; i < len; i++){//注意,循环里不要放strlen!!会很耗很耗时
node[i].c = ss[i];
node[i].num = ;
}
cout << solve() << endl;
return ;
}
Codeforces909D Colorful Points(缩点)的更多相关文章
- Codeforces 909 D. Colorful Points (模拟)
题目链接: Colorful Points 题意: 给出一段字符串(长度最大为1e6),每次操作可以删除字符串中所有相邻字符与其不同的字符.例如:aabcaa 删除一次就变成了aa,就无法再删除了.题 ...
- CodeForces 909D Colorful Points
题解: 暴力,模拟. 把字符串压缩一下,相同的处理成一位,记录下个数,然后暴力模拟即可. #include <bits/stdc++.h> using namespace std; con ...
- Codeforces Round #455 (Div. 2) 909D. Colorful Points
题 OvO http://codeforces.com/contest/909/problem/D CF 455 div2 D CF 909D 解 算出模拟的复杂度之后就是一个很水的模拟题 把字符串按 ...
- Codeforces Round #455 (Div. 2)
Codeforces Round #455 (Div. 2) A. Generate Login 题目描述:给出两个字符串,分别取字符串的某个前缀,使得两个前缀连起来的字符串的字典序在所有方案中最小, ...
- Codeforces Round #455
Generate Login 第二个单词肯定只取首字母 Solution Segments 从1开始的线段和在n结束的线段各自凑一凑,剩下的转化为规模为n-2的子问题. Solution Python ...
- Codeforces Round #455 (Div. 2) D题(花了一个早自习补了昨晚的一道模拟QAQ)
D. Colorful Points You are given a set of points on a straight line. Each point has a color assigned ...
- Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph 并查集
D. Mr. Kitayuta's Colorful Graph Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/ ...
- BZOJ 1051: [HAOI2006]受欢迎的牛 缩点
1051: [HAOI2006]受欢迎的牛 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/ ...
- ITU-T G.1081 IPTV性能监测点 (Performance monitoring points for IPTV)
ITU-T 建议书 G.1081 IPTV性能监测点 Performance monitoring points for IPTV Summary Successful deployment of I ...
随机推荐
- php 操作数据库
$datetoday = date('Y-m-d'); $datetime = $thedate; $data_info = $data; $db = array( 'dsn' => 'mysq ...
- Activity的启动
--摘自<android插件化开发指南> 1.AMS管理着四大组件 2.为什么Hook不能在AMS那边?因为AMS属于android系统,android系统可以被Hook,那就是病毒了.四 ...
- Emoji表情编解码库XXL-EMOJI
<Emoji表情编解码库XXL-EMOJI> 一.简介 1.1 概述 XXL-EMOJI 是一个灵活可扩展的Emoji表情编解码库,可快速实现Emoji表情的编解码. 1.2 特性 1.简 ...
- .net(二)
1.维护数据库的完整性.一致性.你喜欢用触发器还是自写业务逻辑?为什么? 答:尽可能用约束(包括CHECK.主键.唯一键.外键.非空字段)实现,这种方式的效率最好:其次用触发器,这种方式可以保证无论何 ...
- Redis自学笔记:4.3进阶-排序
4.3排序 4.3.1有序集合的集合操作 有序集合没有zinter和zunion命令,使用其他命令实现方法: multi zinterstore tempKey ... zrange tempKey ...
- Topcoder的使用方法
http://acmicpc.info/archives/164?tdsourcetag=s_pctim_aiomsg(大家都推荐的一个指南, 但我觉得不好用) https://www.jianshu ...
- 在Android中调用USB摄像头
在网上找了很长时间,网上的资料基本都是说用外国人写的库,但实际上这个库的案例都是不能直接用的(因为权限问题),并不适合学习. 之后偶然发现有国人把这个库重新封装了,并且有源代码以及中文教程: http ...
- BZOJ.3307.雨天的尾巴(dsu on tree/线段树合并)
BZOJ 洛谷 \(dsu\ on\ tree\).(线段树合并的做法也挺显然不写了) 如果没写过\(dsu\)可以看这里. 对修改操作做一下差分放到对应点上,就成了求每个点子树内出现次数最多的颜色, ...
- 图的封装(C++)
一. 问题说明 1.问题的简单描述 将图和网的的创建和基本操作分封装到class 用来熟悉此种数据结构和基于这种数据结构上的基本算法 采用VS2010编译环境 2.工作安排 二. 源代码 1.文件st ...
- 权限框架Apache Shiro 和 Spring Security
Shiro 首先Shiro较之 Spring Security,Shiro在保持强大功能的同时,还在简单性和灵活性方面拥有巨大优势.Shiro是一个强大而灵活的开源安全框架,能够非常清晰的处理认证.授 ...