FZU2275KMP(字符串匹配)


Problem 2275 Game

Accept: 233    Submit: 850
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

411111 11 1111112345 54321123 123

Sample Output

AliceBobAliceAlice

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.

第一次用KMP算法,还是用的模版。
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int nex[1000005];
char s[1000005];
char t[1000005];
int x,y;
void get_next(char t[])
{
    int i=0,j=-1;
    nex[0]=-1;
    while(i<strlen(t))
    {
        while(-1!=j&&t[i]!=t[j])
            j=nex[j];
        if(t[++i]==t[++j])nex[i]=nex[j];
        else nex[i]=j;
    }
}
int kmp(char s[],char t[])
{
    get_next(t);
    int x=strlen(s);
    int y=strlen(t);
    int i=0,j=0;
    while(i<x&&j<y)
    {
        if(j==-1||t[j]==s[i])
        {
            i++;
            j++;
        }
        else
        {
            j=nex[j];
        }
    }
    if(j==y)
        return 1;
    else
        return 0;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(s,'',sizeof(s));
        memset(t,'',sizeof(t));
        memset(nex,0,sizeof(nex));
        cin>>s>>t;
        x=strlen(s);
        y=strlen(t);
        if(x<y)
        {
            printf("Bob
");
            continue;
        }
        int p=kmp(s,t);
        if(p||!strcmp(t,"0"))
        {
            printf("Alice
");
            continue;
        }
        reverse(t,t+y);
        if(kmp(s,t))
        {
            printf("Alice
");
            continue;
        }
        printf("Bob
");
    }
}

原文地址:https://www.cnblogs.com/da-mei/p/9053353.html