Yin and Yang Stones(思路题)
Problem Description:
A mysterious circular arrangement of black stones and white stones has appeared. Ming has been tasked with balancing the stones so that only one black and one white stone remain.
Ming has two operations for balancing the stones:
- Take some consecutive sequence of stones where there is exactly one more black stone than a white stone and replace the stones with a single black stone
- Take some consecutive sequence of stones where there is exactly one more white stone than black stone and replace the stones with a single white stone
Given a circular arrangement, determine if it is possible for Ming to balance the stones.
Input
Each input will consist of a single test case. Note that your program may be run multiple times on different inputs. The input will consist of a single string ss (1 \le |s| \le 10^5)(1≤∣s∣≤105), with only the characters capital ‘BB’ and ‘WW’. The stones are arranged in a circle, so the first stone and the last stone are adjacent.
Output
Output 11 if it is possible for Ming to balance the stones with his rules. Otherwise, output 00.
题意:有一堆阴阳石,如果连续的一段石头的序列中,白色石头的个数要比黑色的石头的个数多一,就可以用白色的石头代替这段石头序列。反过来对黑色的石头也是一样的。问给出的这段序列石头能不能替换完后只剩下一个白石头和一个黑石头。
思路:读完题后wxy一分钟出思路,我敲完后提交A掉,这恐怖的思维速度,,,赛后有看了看这题目:假设我们用黑色的石头来代替这段序列,我们拿走这段序列的时候黑色的要比白色的多拿一个,然后再放上一个黑色的,这样黑色和白色的石头减少的个数就都是一样多的了。所以要想最后只剩下一个黑色一个白色,就只有开始两种的颜色石头的个数是一样的才可以。
代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 1e5; 4 typedef long long ll; 5 int main() 6 { 7 string str; 8 cin>>str; 9 int w= 0,b = 0; 10 for(int i = 0; i<str.size(); i++) 11 { 12 if(str[i]=='W')w++; 13 else if(str[i]=='B') b++; 14 } 15 if(w == b)cout<<1<<endl; 16 else 17 cout<<0<<endl; 18 return 0; 19 } 20 /* 21 样例输入: 22 WWBWBB 23 WWWWBBW 24 WBBBBBWWBW 25 样例输出: 26 1 27 0 28 0 29 */