Game with a Strip
Game with a Strip
Time limit: 2.0 second
Memory limit: 64 MB
Memory limit: 64 MB
There is a strip 1 × n with two sides. Each square of the strip (their total amount is 2n, nsquares on each side) is painted in one of two colors (let’s call them A and B). Alice and Bob play a game. Alice makes the first turn. On each turn, a player can bend the strip in half with the fold passing on the divisions of the squares (i.e. the turn is possible only if the strip has an even length). Bending the strip can be done either inwards or outwards. If the strip has become completely one color after the next turn, then the winner is the player whose color is it (A refers to Alice, B to Bob). If the current player has no legal moves, but no one has won, the game ends with a draw.
Who will win if both players play optimally? This means that each player tries to win; if it is not possible, to achieve a draw.
Input
The first line contains an integer n that is the length of the strip (1 ≤ n ≤ 5 · 105).
The next two lines contain two strings of letters “A” and “B” with lengths n, describing two sides of the strip. The letters that are under each other, correspond to the different sides of the same square.
Output
If Alice wins, output “Alice”. If Bob wins, output “Bob”. If the game ends with a draw, output “Draw”.
Samples
input | output |
---|---|
4 BBAA BABB |
Bob |
3 AAA BBB |
Draw |
2 AA BB |
Alice |
Notes
In the first example, Alice starts the game with the strip BBAA/BABB. After her turn she can get the strip BB/AA or BB/AB. In both cases, Bob can win by getting the strip B/B.
In the second example, Alice can’t make even the first turn, so the result is a draw.
In the third example, Alice wins by the first move, getting the stripe A/A from the strip AA/BB.
分析:预处理出每个点向右的边界;
然后dfs判断即可;
代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> #include <climits> #include <cstring> #include <string> #include <set> #include <bitset> #include <map> #include <queue> #include <stack> #include <vector> #include <list> #define rep(i,m,n) for(i=m;i<=n;i++) #define mod 1000000007 #define inf 0x3f3f3f3f #define vi vector<int> #define pb push_back #define mp make_pair #define fi first #define se second #define ll long long #define pi acos(-1.0) #define pii pair<int,int> #define Lson L, mid, ls[rt] #define Rson mid+1, R, rs[rt] #define sys system("pause") const int maxn=1e6+10; using namespace std; ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);} ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p%mod;p=p*p%mod;q>>=1;}return f;} inline void umax(int &p,int q){if(p<q)p=q;} inline void umin(int &p,int q){if(p>q)p=q;} inline ll read() { ll x=0;int f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n,m,k,t,to[maxn]; char a[maxn]; int dfs(int l,int r,int cnt) { if(to[l]>=r) { if(a[l]=='A')return 1; else return 2; } if((r-l+1)/2%2==1)return 0; int mid=l+r>>1,a=dfs(l,mid,cnt^1),b=dfs(mid+1,r,cnt^1); if(cnt==0) { if(a==1||b==1)return 1; else if(!a||!b)return 0; else return 2; } else { if(a==2||b==2)return 2; else if(!a||!b)return 0; else return 1; } } int main() { int i,j; scanf("%d%s",&n,a); scanf("%s",a+n); n<<=1; for(i=n-1;i>=0;i--) { if(a[i]==a[i+1])to[i]=to[i+1]; else to[i]=i; } int ok=dfs(0,n-1,0); if(ok==0)puts("Draw"); else if(ok==1)puts("Alice"); else puts("Bob"); return 0; }