大概就是dfs?当前区间(l,r)的答案可以由(l,m)和(m+1,r)区间推出,如果某个区间已经完全被某种颜色覆盖,那么就返回该颜色。否则按照递归层数判定,奇数层Alice占优势,只需左右区间中一者为必胜即可,而Bob需要左右区间均为其必胜色才可以。无解判一下即可。感觉还是很巧妙。

There is a strip 1 × n with two sides. Each square of the strip (their total amount is 2 nn squares on each side) is painted in one of two colors (let’s call them Aand 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 ( Arefers 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 · 10 5).
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”.

Example

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.
#include<cstdio>
int n,rsz[1000010];
char a[1000010];
int dfs(int l,int r,int dep)
{
if(rsz[l]>=r)
{
if(a[l]=='A')
return 1;
if(a[l]=='B')
return 2;
return 0;
}
if(((r-l+1)/2)%2!=0)
return 0;
int m=(l+r>>1);
int f1=dfs(l,m,dep^1),f2=dfs(m+1,r,dep^1);
if(!dep)
{
if(f1==1 || f2==1)
return 1;
if(f1==0 || f2==0)
return 0;
// if(f1==2 && f2==2)
return 2;
}
else
{
if(f1==2 || f2==2)
return 2;
if(f1==0 || f2==0)
return 0;
// if(f1==1 && f2==1)
return 1;
}
}
int main()
{
//freopen("e.in","r",stdin);
scanf("%d",&n);
scanf("%s",a+1);
scanf("%s",a+1+n);
n<<=1;
rsz[n]=n;
for(int i=n-1;i>=1;--i)
if(a[i]==a[i+1])
rsz[i]=rsz[i+1];
else
rsz[i]=i;
int f=dfs(1,n,0);
if(f==0)
puts("Draw");
else if(f==1)
puts("Alice");
else
puts("Bob");
return 0;
}

【DFS】URAL - 2104 - Game with a Strip的更多相关文章

  1. 【BZOJ1814】Ural 1519 Formula 1 (插头dp)

    [BZOJ1814]Ural 1519 Formula 1 (插头dp) 题面 BZOJ Vjudge 题解 戳这里 上面那个链接里面写的非常好啦. 然后说几个点吧. 首先是关于为什么只需要考虑三进制 ...

  2. 【第40套模拟题】【noip2011_mayan】解题报告【map】【数论】【dfs】

    目录:1.潜伏者 [map] 2.Hankson的趣味题[数论]3.mayan游戏[dfs] 题目: 1. 潜伏者(spy.pas/c/cpp)[问题描述]R 国和S 国正陷入战火之中,双方都互派间谍 ...

  3. 【BZOJ1814】Ural 1519 Formula 1 插头DP

    [BZOJ1814]Ural 1519 Formula 1 题意:一个 m * n 的棋盘,有的格子存在障碍,求经过所有非障碍格子的哈密顿回路个数.(n,m<=12) 题解:插头DP板子题,刷板 ...

  4. Kattis - glitchbot 【DFS】

    Kattis - glitchbot [DFS] 题意 有一个机器人 刚开始在(0, 0),然后给出一个目标点,并且会给出一系列指令,但是其中会有一个指令是错误的.我们需要找出那个指令,并且改成正确的 ...

  5. HDU 6113 度度熊的01世界 【DFS】(2017"百度之星"程序设计大赛 - 初赛(A))

    度度熊的01世界 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  6. 【dfs】P1331 海战

    题目描述 在峰会期间,武装部队得处于高度戒备.警察将监视每一条大街,军队将保卫建筑物,领空将布满了F-2003飞机.此外,巡洋船只和舰队将被派去保护海岸线.不幸的是因为种种原因,国防海军部仅有很少的几 ...

  7. 【dfs】p1731 生日蛋糕

    1441:[例题2]生日蛋搞 [题目描述] 7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱体.设从下往上数第i(1≤i≤M)层蛋糕是半径为Ri, 高 ...

  8. 【dfs】LETTERS

    1212:LETTERS [题目描述] 给出一个roe×colroe×col的大写字母矩阵,一开始的位置为左上角,你可以向上下左右四个方向移动,并且不能移向曾经经过的字母.问最多可以经过几个字母. [ ...

  9. 洛谷P1605 迷宫【dfs】

    题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫 中移动有上下 ...

随机推荐

  1. fastjson对json操作

    fastjson对json字符串JSONObject和JSONArray互相转换操作示例  fastjson的方法: Fastjson API入口类是com.alibaba.fastjson.JSON ...

  2. 普通table表格样式及代码大全

     普通table表格样式及代码大全(全)(一) 单实线边框表格 <table style="border-collapse: collapse" borderColor=#0 ...

  3. Java环境变量配置以及作用、JDK与JRE区别以及命令行引入jar包

    在配置环境变量中: 设置JAVA_HOME: 一是为了方便引用,比如,JDK安装在C:\jdk1.6.0目录里,则设置JAVA_HOME为该目录路径, 那么以后要使用这个路径的时候, 只需输入%JAV ...

  4. OpenWRT介绍

    1. 介绍 OpenWRT是一款第三方路由器固件, 其特别在于开放性, 如它的文件系统可写, 用户可在路由器上安装需要的第三方软件.通过刷入OpenWRT, 我们可以完成如下事情 - DLNA共享 - ...

  5. hibernate基础学习

    转载自:http://blog.csdn.net/fb281906011/article/details/17628111 一:下载hibernate:http://hibernate.org/orm ...

  6. snakes

    原地址 讨论区 Changing 算法一 我会随机! 由于我忘了设置多组数据,期望得分0至100. 算法二 我会模拟! 复杂度\(O(t^2)\),期望得分60. 但是很多人忘记题目给出的是环形-- ...

  7. 定义序列化器时的read_only和write_only选项

    # 转载请留言联系 read_only read_only表示只能读,不能进行修改.例如定义序列化器时,id字段通常指定read_only=True.在序列化时,即对象转为字典.JSON字符串时,字典 ...

  8. Corosync 配置描述

    NAME corosync.conf - corosync executive configuration file SYNOPSIS /etc/corosync/corosync.conf DESC ...

  9. 看不到Harbor我也睡不着觉啊

    上午打球,下午陪小孩子看上海科技展,晚上搞定harbor. 完美!!!:) 参考文档: https://www.dwhd.org/20161023_110618.html http://blog.cs ...

  10. 如何php防止XSS攻击

    什么是XSS:这里通俗的讲,就像是SQL注入一样,XSS攻击也可以算是对HTML和JS的一种注入.你本来希望得到是从用户那得到一段有用的文本文字,但用户提交给你的却是别有用心的可执行javascrip ...