fzu Problem 2275 Game(kmp)
2017-07-24 11:01
330 查看
Problem 2275 Game
Accept: 62 Submit: 165
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”.
4
11111 1
1 11111
12345 54321
123 123
Alice
Bob
Alice
Alice
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.
第八届福建省大学生程序设计竞赛-重现赛(感谢承办方厦门理工学院)
题解:kmp
如果b的位数大于a,B获胜。如果B为0,A获胜。如果b是a的逆序,A获胜,其他情况,如果b是a或者逆序a的子串,A获胜。
Accept: 62 Submit: 165
Time Limit: 1000 mSec Memory Limit : 262144 KB
![](http://acm.fzu.edu.cn/image/prodesc.gif)
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.
![](http://acm.fzu.edu.cn/image/prodesc.gif)
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.
![](http://acm.fzu.edu.cn/image/prodesc.gif)
Output
For each test case, if Alice can win the game, output “Alice”. Otherwise output “Bob”.
![](http://acm.fzu.edu.cn/image/prodesc.gif)
Sample Input
411111 1
1 11111
12345 54321
123 123
![](http://acm.fzu.edu.cn/image/prodesc.gif)
Sample Output
AliceBob
Alice
Alice
![](http://acm.fzu.edu.cn/image/prodesc.gif)
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.
![](http://acm.fzu.edu.cn/image/prodesc.gif)
Source
第八届福建省大学生程序设计竞赛-重现赛(感谢承办方厦门理工学院)题解:kmp
如果b的位数大于a,B获胜。如果B为0,A获胜。如果b是a的逆序,A获胜,其他情况,如果b是a或者逆序a的子串,A获胜。
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> using namespace std; char s[1000005],t[1000005],tmp[1000005]; int nextl[1000005]; int ls,lt; void getnext() { nextl[0]=-1; for(int i=1;i<lt;i++) { int j=nextl[i-1]; while(t[j+1]!=t[i]&&j>-1) j=nextl[j]; nextl[i]=(t[j+1]==t[i])?j+1:-1; } } int kmp(char *a,char *b) { getnext(); int sum=0,i=0,j=0; while(i<ls&&j<lt) { if(j==-1||a[i]==b[j]) i++,j++; else j=nextl[j]; } if(j==lt) return 1; return 0; } int main() { int T; scanf("%d",&T); while(T--) { scanf("%s%s",&s,&t); ls = strlen(s); lt = strlen(t); if (lt>ls) { printf("Bob\n"); continue; } if(kmp(s,t) || !strcmp(t,"0")) {printf("Alice\n"); continue;} //如果t字符串是0,那么一定是Alice赢 reverse(t,t+lt); //这个翻转用法第一次碰到 if(kmp(s,t)) {printf("Alice\n"); continue;} printf("Bob\n"); } return 0; }
相关文章推荐
- 【FZU - 2275 】Game 【KMP and 博弈】
- 【FZU - 2275 Game】 kmp
- FZU 2275 Game(KMP)
- FZU 2275 Game (KMP)
- FZU Problem 2151 OOXX Game (数学啊)
- fzu2275-kmp
- FZU-Problem 2147 A-B Game
- FZU_Problem 2151 OOXX Game
- FZU 2275 Game【博弈】
- fzu 2275 Game
- Problem_FZU 2150 Fire Game BFS 两点
- Fzu Problem 1901 Period II (kmp)
- FZU Problem 1926 填空(KMP好题一枚,确实好)
- FZU 2037 Maximum Value Problem【递推】
- FZU Problem 1534 阿甘的珠宝 (博弈)
- FZU Problem 2150 Fire Game
- FZU-2218 Simple String Problem
- FZU Fire Game(两点BFS)
- 【计算几何】FZU Problem 2270 Two Triangles
- foj Problem 2275 Game