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 to it. For point a, its neighbors are the points which don‘t have any other points between them and a. Each point has at most two neighbors - one from the left and one from the right.

You perform a sequence of operations on this set of points. In one operation, you delete all points which have a neighbor point of a different color than the point itself. Points are deleted simultaneously, i.e. first you decide which points have to be deleted and then delete them. After that you can perform the next operation etc. If an operation would not delete any points, you can‘t perform it.

How many operations will you need to perform until the next operation does not have any points to delete?

Input
Input contains a single string of lowercase English letters ‘a‘-‘z‘. The letters give the points‘ colors in the order in which they are arranged on the line: the first letter gives the color of the leftmost point, the second gives the color of the second point from the left etc.

The number of the points is between 1 and 106.

Output
Output one line containing an integer - the number of operations which can be performed on the given set of points until there are no more points to delete.

Input

aabb

Output

2

Input

aabcaa

Output

1

思路:模拟一下

AC代码:

 1 #include<bits/stdc++.h>
 2
 3 using namespace std;
 4 vector< pair<int,int> > v;
 5 int main(){
 6     string str;
 7     cin>>str;
 8     int num=1;
 9     int flag=1;
10     for(int i=0;i<=str.size();i++){
11         if(str[i]!=str[i+1]&&(i+1)<str.size()){
12             flag=0;
13         }
14         if(str[i]!=str[i+1]){
15             v.push_back(make_pair(str[i]-‘a‘,num));
16             num=1;
17         }else{
18             num++;
19         }
20     }
21     if(flag){
22         printf("0");
23         return 0;
24     }
25     /*vector<pair<int,int> > ::iterator it;
26     for(it=v.begin();it!=v.end();it++){
27         cout<<(*it).first<<" "<<(*it).second<<endl;
28     }
29     */
30     int ans=0;//aabcaa
31
32     while(1){
33          vector<pair<int,int> > ::iterator it;
34          for(it=v.begin();it!=v.end();it++){
35              if(it==v.begin()||it==(v.end()-1)){
36                  (*it).second--;
37                  continue;
38              }else{
39                  (*it).second-=2;
40              }
41          }
42         vector<pair<int,int> > ::iterator xit;
43         /*for(xit=v.begin();xit!=v.end();xit++){
44             cout<<(*xit).first<<" "<<(*xit).second<<endl;
45         }
46         */
47         vector< pair<int,int> > temp;
48         vector<pair<int,int> > ::iterator itt;
49         for(itt=v.begin();itt!=(v.end());itt++){
50              if((*itt).second>0){
51                  if(temp.size()==0){
52                      temp.push_back(make_pair((*itt).first,(*itt).second));
53                  }else{
54                      vector<pair<int,int> > ::iterator t=temp.end()-1;
55                      if((*itt).first==(*t).first){
56                          (*t).second+=(*itt).second;
57                     }else{
58                          temp.push_back(make_pair((*itt).first,(*itt).second));
59                     }
60                  }
61             }
62         }
63         /*vector<pair<int,int> > ::iterator flag=temp.begin();
64
65         cout<<temp.size()<<endl;
66         for(;flag!=temp.end();flag++){
67             cout<<(*flag).first<<" "<<(*flag).second<<endl;
68         }
69         */
70         ans++;
71         if(temp.size()<=1){
72             break;
73         }else{
74             v=temp;
75         }
76
77     }
78     cout<<ans<<endl;
79     return 0;
80 }
81
82 /*
83
84 aabbaaa
85
86 */

原文地址:https://www.cnblogs.com/pengge666/p/11539762.html

时间: 2024-10-05 22:25:22

Codeforces Round #455 (Div. 2) D题(花了一个早自习补了昨晚的一道模拟QAQ)的相关文章

Codeforces Round #257 (Div. 2) E题:Jzzhu and Apples 模拟

E. Jzzhu and Apples time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Jzzhu has picked n apples from his big apple tree. All the apples are numbered from 1 to n. Now he wants to sell them to

Codeforces Round #243 (Div. 1) A题

http://codeforces.com/contest/425/problem/A 题目链接: 然后拿出这道题目是很多人不会分析题目,被题目吓坏了,其中包括我自己,想出复杂度,一下就出了啊!真是弱! 直接暴力求出矩阵数值,然后枚举每一个[I,J];再O[N]判断,分配好在[I,J]区间的数和之内的数,再排序下SOLO了 CODE:#include <cstdio> #include <cstring>#include <queue>#include <vect

Codeforces Round #455 (Div. 2) C. Python Indentation dp递推

Codeforces Round #455 (Div. 2) C. Python Indentation 题意:python 里面,给出 n 个 for 循环或陈述语句,'f' 里面必须要有语句.按 python 缩进的方式组合成合法的程序,问有多少种可能方案. tags: dp dp[i][j] 表示第 i 个语句缩进为 j 时的可能方案数, 转移: 1] 如果第 i 个是 'f' , 则第 i+1 个肯定要比第 i 个多缩进一个单位,即 dp[i+1][j] = dp[i][j]. 2]如果

Codeforces Round #267 (Div. 2) C. George and Job(DP)补题

Codeforces Round #267 (Div. 2) C. George and Job题目链接请点击~ The new ITone 6 has been released recently and George got really keen to buy it. Unfortunately, he didn't have enough money, so George was going to work as a programmer. Now he faced the follow

Codeforces Round #396 (Div. 2) D题Mahmoud and a Dictionary(并查集)解题报告

Mahmoud wants to write a new dictionary that contains n words and relations between them. There are two types of relations: synonymy (i. e. the two words mean the same) and antonymy (i. e. the two words mean the opposite). From time to time he discov

Codeforces Round #374 (div.2)遗憾题合集

C.Journey 读错题目了...不是无向图,结果建错图了(喵第4样例是变成无向就会有环的那种图) 并且这题因为要求路径点尽可能多 其实可以规约为限定路径长的拓扑排序,不一定要用最短路做 #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cmath> #include<algorithm>

Codeforces Round #297 (Div. 2) E题. Anya and Cubes (中途相遇法)

题目地址:Anya and Cubes 比赛的时候居然没想起中途相遇法...这题也是属于想起来就很简单系列. 中途相遇法也叫折半搜索.就是处理前一半,把结果储存起来,再处理后一半,然后匹配前一半存储的结果. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib

Codeforces Round #367 (Div. 2) 套题

吐槽:只能说是上分好场,可惜没打,唉 A:Beru-taxi (水题,取最小值) #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string>

Codeforces Round #297 (Div. 2) D题. Arthur and Walls(BFS)

题目地址:Arthur and Walls 这题有一个脑洞,对于当前的点(i,j)并且此点为"*"来说,若存在包含它的2*2正方形中除了它自己外,另外三个点都是".",那么这个点就必须要变成".".由于去掉这个点之后会对周围的8个点造成影响,所以可以用BFS去搜.WA第12组的应该是只考虑了会影响到周围的4个点了. 代码如下: #include <iostream> #include <string.h> #include