Problem D Game

Accept: 145    Submit: 844
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.

题意:两个人操作各自的字符串,如果Alice通过有限的操作能得到和Bob一样的字符串,那么Alice赢,否则Bob赢

思路:仔细想想可以发现如果Bob的字符串如果是Alice的子串,那么Alice一定能通过有限的操作最终得到和Bob一样的字符串。

KMP字符串匹配即可,Alice的字符串以及其反串都判断一遍,注意在判断反串之前把反串中的前导0去掉。

AC代码:

  1. #define _CRT_SECURE_NO_DEPRECATE
  2. #include<iostream>
  3. #include<string>
  4. #include<algorithm>
  5. using namespace std;
  6. int Next[+] = { };
  7. /* P为模式串,下标从0开始 */
  8. void GetNext(string P, int next[])
  9. {
  10. int p_len = P.size();
  11. int i = ; //P的下标
  12. int j = -;
  13. next[] = -;
  14.  
  15. while (i < p_len)
  16. {
  17. if (j == - || P[i] == P[j])
  18. {
  19. i++;
  20. j++;
  21. next[i] = j;
  22. }
  23. else
  24. j = next[j];
  25. }
  26. }
  27.  
  28. /* 在S中找到P第一次出现的位置 */
  29. int KMP(string S, string P, int next[]){
  30. GetNext(P, next);
  31.  
  32. int i = ; //S的下标
  33. int j = ; //P的下标
  34. int s_len = S.size();
  35. int p_len = P.size();
  36.  
  37. while (i < s_len && j < p_len)
  38. {
  39. if (j == - || S[i] == P[j]) //P的第一个字符不匹配或S[i] == P[j]
  40. {
  41. i++;
  42. j++;
  43. }
  44. else
  45. j = next[j]; //当前字符匹配失败,进行跳转
  46. }
  47.  
  48. if (j == p_len) //匹配成功
  49. return i - j;
  50.  
  51. return -;
  52. }
  53. string s1, s2;
  54. int main(){
  55. int t;
  56. scanf("%d",&t);
  57. while (t--) {
  58. cin >> s1 >> s2;
  59. if (KMP(s1, s2, Next) != -) { printf("Alice\n"); continue; }
  60. else {
  61. string tmp; bool flag = ; int k;
  62. reverse(s2.begin(), s2.end());
  63. for (k = ; k < s2.size();k++) //去掉前导0
  64. if (s2[k] != '')break;
  65. tmp = s2.substr(k, s2.size());
  66. if (KMP(s1, tmp, Next) != -)printf("Alice\n");
  67. else printf("Bob\n");
  68. }
  69. }
  70.  
  71. return ;
  72. }

foj Problem 2275 Game的更多相关文章

  1. fzu Problem 2275 Game(kmp)

    Problem 2275 Game Accept: 62    Submit: 165Time Limit: 1000 mSec    Memory Limit : 262144 KB  Proble ...

  2. FOJ ——Problem 1759 Super A^B mod C

     Problem 1759 Super A^B mod C Accept: 1368    Submit: 4639Time Limit: 1000 mSec    Memory Limit : 32 ...

  3. FOJ Problem 1016 无归之室

     Problem 1016 无归之室 Accept: 926    Submit: 7502Time Limit: 1000 mSec    Memory Limit : 32768 KB  Prob ...

  4. FOJ Problem 1015 土地划分

    Problem 1015 土地划分 Accept: 823    Submit: 1956Time Limit: 1000 mSec    Memory Limit : 32768 KB  Probl ...

  5. foj Problem 2107 Hua Rong Dao

    Problem 2107 Hua Rong Dao Accept: 503    Submit: 1054Time Limit: 1000 mSec    Memory Limit : 32768 K ...

  6. foj Problem 2282 Wand

     Problem 2282 Wand Accept: 432    Submit: 1537Time Limit: 1000 mSec    Memory Limit : 262144 KB Prob ...

  7. FOJ Problem 2273 Triangles

    Problem 2273 Triangles Accept: 201    Submit: 661Time Limit: 1000 mSec    Memory Limit : 262144 KB P ...

  8. foj Problem 2283 Tic-Tac-Toe

                                                                                                    Prob ...

  9. FOJ Problem 2257 Saya的小熊饼干

                                                                                                        ...

随机推荐

  1. 安装搭配VUE使用的UI框架ElementUI

    可以搭配vue的UI框架有几个,我用的是element-ui,现在呢,我要在复习一遍 1.vue init webpack-simple element-ui2.cd element-ui3.npm ...

  2. ubuntu k8s 命令补全

    apt install bash-completion // locate bash_completion source /usr/share/bash-completion/bash_complet ...

  3. Spring多种方式实现依赖注入

    平常的Java开发中,程序员在某个类中需要依赖其它类的方法. 通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理. Spring提出了依赖注入的思想,即依赖类不由 ...

  4. Bzoj 近期题目一句话题解

    目录 Bzoj 近期题目题解 1000: A+B Problem (模拟) 1008: [HNOI2008]越狱 (容斥) 1012: [JSOI2008]最大数maxnumber (线段树) 103 ...

  5. springboot下https证书配置

    没有证书的小伙伴首先申请一个阿里云免费证书,按照我的步骤来操作 1.购买页面是这样的 按照顺序选择 神奇的一幕出现了 然后就去购买成功,我们会看到证书没有签发,我们需要去申请 填写需要绑定的域名 一般 ...

  6. 20181207(sys,shelve,logging)

    一.logging模块 logging专门用来记录日志 日志的级别分为五级,可以用数字表示,从低到高分别为: import  logginglogging.info('info')   #10logg ...

  7. shell-code-拷贝文件

    #!/bin/bash while read F do cp ${F}"_pe_1.fastq.gz" /public/home/chenjy/usr/ZD/data/cleand ...

  8. Java-basic-1

    1. Java Standard Edition (Java SE) Java Enterprise Edition (Java EE): geared toward developing large ...

  9. ProC第一弹

    编译pro*c 的makefile例子 原来只需在makefile中追加include $(ORACLE_HOME)/precomp/lib/env_precomp.mk,其他一切按照makefile ...

  10. isinstance() 函数

    Python3 isinstance() 函数   描述 isinstance() 函数来判断一个对象是否是一个已知的类型,类似 type(). isinstance() 与 type() 区别: t ...