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