1. 程式人生 > >Yin and Yang Stones(思路題)

Yin and Yang Stones(思路題)

enc last cas black ear ive close script test

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:

  1. 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
  1. 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)(1s105), 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掉,這恐怖的思維速度,,,賽後有看了看這題目:假設我們用黑色的石頭來代替這段序列,我們拿走這段序列的時候黑色的要比白色的多拿一個,然後再放上一個黑色的,這樣黑色和白色的石頭減少的個數就都是一樣多的了。所以要想最後只剩下一個黑色一個白色,就只有開始兩種的顏色石頭的個數是一樣的才可以。

代碼:

技術分享圖片
 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 */
View Code

Yin and Yang Stones(思路題)