【FZU
来源:互联网 发布:php bbs论坛源码 编辑:程序博客网 时间:2024/06/05 08:21
D - Game
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.
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.
For each test case, if Alice can win the game, output “Alice”. Otherwise output “Bob”.
411111 11 1111112345 54321123 123
AliceBobAliceAlice
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.
题意:总结一下就是,给出两个数字a、b,在a进行去首(删除最高位数字)或者去尾(删除最低位数字)操作后能否得到数字b。
分析:把a、b数字作为字符串处理,在a中kmp查找是否有b以及b的逆序串,需要注意的是当b为0时需要进行特判,输出Alice。
代码如下:
#include <map>#include <cmath>#include <queue>#include <stack>#include <vector>#include <cstdio>#include <string>#include <cstring>#include <iostream>#include <algorithm>#define LL long long#define INF 0x3f3f3f3fusing namespace std;const int MX = 1e5 + 5;int n, m;int nxt[MX];char a[MX], b[MX], c[MX];void get_nxt(){ int i = 0, j = -1; nxt[0] = -1; while(i < m){ if(j == -1 || a[i] == a[j]){ nxt[++i] = ++j; } else j = nxt[j]; }}int kmp1(){ int i = -1, j = -1; while(i < n && j < m){ if(j == -1 || a[i] == b[j]){ i++, j++; } else j = nxt[j]; } if(j == m) return 1; return 0;}int kmp2(){ int i = -1, j = -1; while(i < n && j < m){ if(j == -1 || a[i] == c[j]){ i++, j++; } else j = nxt[j]; } if(j == m) return 1; return 0;}int main(){ int t; scanf("%d", &t); while(t--){ scanf("%s", a); scanf("%s", b); n = strlen(a); m = strlen(b); if(b[0] == '0'){ puts("Alice"); continue; } for(int i = 0; i < m; i++){ c[i] = b[m-1-i]; } get_nxt(); int ans1 = kmp1(); int ans2 = kmp2(); if(ans1 || ans2) puts("Alice"); else puts("Bob"); } return 0;}
- FZU
- FZU
- FZU
- FZU
- FZU
- FZU
- FZU
- FZU
- FZU
- FZU
- FZU
- FZU
- FZU
- FZU
- FZU
- FZU
- FZU
- FZU
- 文件系统与目录树的关系
- 图的遍历、最小生成树、最短路径
- 简单的http协议
- java调用百度地图API通过URL根据地理位置中文处理返回值获取经纬度
- EasyPusher 华为手机硬编码 [OMX.IMG.TOPAZ.Encoder] failed to set input port definition parameters.
- 【FZU
- 2697: 特技飞行
- SiteMesh3简介及使用
- Android MVP 详解(上)
- [HDU-2612] Find a Way
- caffe 参数的详解solver文件<一>
- C#中如何使用水晶报表
- Windows 7下用VMware Workstation 10虚拟机安装 Ubuntu 14.04
- 如何尽可能麻烦的通过open cv控制一个小灯