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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值