本文主要是介绍fzu 2275 Game KMP,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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
Sample Output
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
第八届福建省大学生程序设计竞赛-重现赛(感谢承办方厦门理工学院)0特判
import java.io.BufferedInputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;public class Main {public static void main(String[] args) {new Task().solve();}
}class Task {Scanner in = new Scanner(new BufferedInputStream(System.in)) ;PrintWriter out = new PrintWriter(System.out);void solve(){int t = in.nextInt() ;while(t-- > 0){String A = in.next() ; String B = in.next() ;KMP kmp = new KMP(B.toCharArray()) ;if('0' == B.charAt(0) || kmp.searchFrom(A.toCharArray()) || kmp.searchFrom(new StringBuffer(A).reverse().toString().toCharArray())){out.println("Alice") ;}else{out.println("Bob") ; }}out.flush() ;}}class KMP{int m ;char[] p ;int[] fail ;KMP(char[] p){m = p.length ;this.p = p ;fail = new int[m+1] ;int cur = fail[0] = -1 ;for(int i = 1 ; i <= m ; i++){while(cur >= 0 && p[cur] != p[i-1]){cur = fail[cur] ;}fail[i] = ++cur ;}}boolean searchFrom(char[] t){int n = t.length ;for(int i = 0 , cur = 0 ; i < n ; i++){while(cur >= 0 && t[i] != p[cur]){cur = fail[cur] ;}if(++cur == m){return true ;}}return false ;}}
这篇关于fzu 2275 Game KMP的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!