解题报告:问Alice的串是否能通过反转以及整除10的操作达到和Bob的串一样。相当于Bob的串只要是Alice的串的子串Alice就能赢。特别的,Bob的串为0的话Alice一定能赢!

Code

#include <stdio.h>
#include <string.h>
using namespace std;
const int maxn = (int)1e5+5;
char sa[maxn],pattern[maxn],saa[maxn];
int prefix[maxn];

void mystr(char str[], int n) {
	int i,j;
	char tmp;
	i = 0;
	j = n-1;
	while (i < j) {
		tmp = str[i];
		str[i] = str[j];
		str[j] = tmp;
		i++;
		j--;
	}
}

void get_prefix_table (int n) {
    int i = 0, len = -1;
    prefix[0] = -1;
    while (i < n) {
        if(len == -1 || pattern[i] == pattern[len]) {
            i++;
            len++;
            prefix[i] = len;
        }else {
            len = prefix[len];
        }
    }
}

int kmp_search (int n, int m, char Tmp[]) {
    int ans = 0, i = 0, j = 0;
    while (i < n) {
        if(j == -1 || Tmp[i] == pattern[j]) {
            i++;
            j++;
        }else {
            j = prefix[j];
        }
        if(j == m) {
            ans++;
            j = prefix[j];
        }
    }
    return ans;
}

int main() {
	int T;
	scanf("%d", &T);
	while (T--) {
		getchar();
		scanf("%s%s", sa, pattern);
		int l1 = strlen(sa);
		int l2 = strlen(pattern);
		if (l1 < l2) {
			printf("Bob\n");
			continue;
		}
		strcpy(saa, sa);
		mystr(saa, l1);
		get_prefix_table (l2);
		int k1 = kmp_search(l1, l2, sa);
		int k2 = kmp_search(l1, l2, saa);
		if (k1 != 0 || k2 != 0 || l2 == 1 && pattern[0] == '0') {
			printf("Alice\n");
		} else {
			printf("Bob\n");
		}
	}
	return 0;
}