D - Game 2019省赛训练组队赛3.31周四-17fj

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.


提交Java代码时加了package就wrong 了,新学了kmp算法
import java.util.Scanner;

public class Main {

	public static int [] getNext(String ps) {
		char [] p = ps.toCharArray();
		int [] next = new int [p.length];
		next[0]=-1;
		int j=0;
		int k=-1;
		while(j<p.length-1) {
			if(k==-1||p[j]==p[k]) {
				next[++j]=++k;
			}
			else {
				k=next[k];
			}
		}
		return next;
	}
	public static int KMP(String ps,String ts) {
		String c = new StringBuffer(ts).reverse().toString();
		if(ps.equals(ts)||ps.equals(c)) return 1;
		char [] p = ps.toCharArray();
		char [] t = ts.toCharArray();
		int i=0;
		int j=0;
		int [] next = getNext(ts);
		while(i<p.length&&j<t.length) {
			if(j==-1||p[i]==t[j]) {
				i++;
				j++;
			}
			else {
				j=next[j];
			}
		}
		if(j==t.length) {
			return i-j;
		}
		else {
			return -1;
		}
	}
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n;
		n=in.nextInt();
		for( int i=0; i<n; i++ ) {
			String a,b;
			a=in.next();b=in.next();
			String c = new StringBuffer(b).reverse().toString();
			if(b.equals("0")&&b.length()==1) {
				System.out.println("Alice");continue;
			}
			if(a.length()>=b.length()) {
				if(KMP(a, b)!=-1||KMP(a, c)!=-1) System.out.println("Alice");
				else System.out.println("Bob");
			}
			else System.out.println("Bob");
		}
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_43323172/article/details/88744377
今日推荐