Problem 2275 Game

Accept: 159    Submit: 539
Time Limit: 1000 mSec    Memory Limit : 262144 KB

 Problem Description

Alice and Bob is playing a game.

Each of them has a number. Alice’s number is A, and Bob’s number is B.

Each turn, one player can do one of the following actions on his own number:

1. Flip: Flip the number. Suppose X = 123456 and after flip, X = 654321

2. Divide. X = X/10. Attention all the numbers are integer. For example X=123456 , after this action X become 12345(but not 12345.6). 0/0=0.

Alice and Bob moves in turn, Alice moves first. Alice can only modify A, Bob can only modify B. If A=B after any player’s action, then Alice win. Otherwise the game keep going on!

Alice wants to win the game, but Bob will try his best to stop Alice.

Suppose Alice and Bob are clever enough, now Alice wants to know whether she can win the game in limited step or the game will never end.

 Input

First line contains an integer T (1 ≤ T ≤ 10), represents there are T test cases.

For each test case: Two number A and B. 0<=A,B<=10^100000.

 Output

For each test case, if Alice can win the game, output “Alice”. Otherwise output “Bob”.

 Sample Input

4
11111 1
1 11111
12345 54321
123 123

 Sample Output

Alice
Bob
Alice
Alice

 Hint

For the third sample, Alice flip his number and win the game.

For the last sample, A=B, so Alice win the game immediately even nobody take a move.

 Source

第八届福建省大学生程序设计竞赛-重现赛(感谢承办方厦门理工学院)

博弈问题,有两种操作,除10,或者反转,只要任何一方操作后,得到的两个数字相同,在有限步结束则Alice胜,否则Bob胜,
样例1:Alice不断除10,Bob只能除10或者反转,总会和Alice出现相同的情况;
样例2:Alice随意操作,Bob只需反转即可,而这不可能相同;
样例3:Alice反转即可;
样例4:不用操作,Alice胜;
可以看出,如果Bob的序列长度大于Alice,Alice肯定输,如果Bob的序列是Alice的子串,为了避免和Alice一致,只能除10,最后到0时Alice胜,如果不是子串,Bob不断反转即可,这就变成了子串匹配问题,可以用KMP算法,对Bob正匹配一次,反转匹配一次,要注意的是前导0,因为这个WA了。。。
#include<iostream>
#include<cstring>
#include<cstdio>
#define _match(a,b) ((a)==(b))
using namespace std;
const int N = + ;
typedef char elem_t;
char A[N],B[N],C[N];
int fail[N]; int pat_match(int ls,elem_t* str,int lp,elem_t* pat){
int i,j;
fail[] = -;
for(j=;j<lp;j++){
for(i=fail[j-];i>=&&!_match(pat[i+],pat[j]);i=fail[i]);
fail[j] = (_match(pat[i+],pat[j])?i+:-);
}
for(i=j=;i<ls && j<lp ;i++)
if(_match(str[i],pat[j]))
j++;
else if(j)
j=fail[j-]+,i--;
return j==lp?(i-lp):-;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%s",A);
scanf("%s",B);
int lena=strlen(A);
int lenb=strlen(B);
if(lena < lenb){printf("Bob\n"); continue;}
if(B[lenb-]=='') {printf("Alice\n"); continue;}
if(pat_match(lena,A,lenb,B)>=){printf("Alice\n");continue;}
int k=;
for(int i=lenb-;i>=;i--)
C[k++] = B[i];
k=;
while(C[k]=='') {k++;lenb--;}
if(pat_match(lena,A,lenb,C+k)>=){printf("Alice\n");continue;}
printf("Bob\n");
}
}

最新文章

  1. Hibernatel框架关联映射
  2. Mysql查看执行计划-explain
  3. XML解析工具类
  4. 关于silverlight打印模糊的问题
  5. 10TSQL语言概述-脚本调试-命名规范-天轰穿数据库2014
  6. CAPI3 HTTP文件服务器搭建(共享目录版)
  7. IE6、IE7、IE8中overflow:hidden无效问题
  8. web工程中URL地址的写法
  9. Borland license information was found,but it is not valid for delphi.
  10. 如何将js与HTML完全脱离
  11. Oracle Dedicated server 和 Shared server(专用模式 和 共享模式) 说明(转)
  12. 中国还是和AMD走到一起了 但美国会高兴吗(网易科技 卢鑫)
  13. JAVA 命令行参数解析,org.apache.commons.cli的使用
  14. EasyUI Tree 树 ——实现多级别菜单的展示,以及与后台数据的交互
  15. [Css] css3的flex布局
  16. flask 数据库迁移的简单操作
  17. Python数据库连接池DBUtils
  18. 苹果审核被拒,解析奔溃日志.txt转crash文件
  19. Linux下安装解压版(tar.gz)MySQL5.7
  20. PHP实现IP访问限制及提交次数的方法详解

热门文章

  1. sharepoint 2010 创建自定义的ASP.NET Web Service (上)
  2. heroinfo_set.all 函数
  3. 进行移动端rem适配
  4. SQL Server2008收缩日志文件
  5. MIF文件生成说明
  6. asp.net 怎么上传文件夹啊,不传压缩包
  7. 如何在Web页面里使用高拍仪扫描上传图像
  8. 【bzoj1927】[Sdoi2010]星际竞速
  9. BP算法演示
  10. [CSP-S模拟测试]:矩阵游戏(数学)