Accept: 18 Submit: 56
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
AliceBobAliceAlice
核心是kmp。。。。。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
char a[1111111];
char b[1111111];
int next[1111111];
int la,lb;
void getn()
{
int i=0,j;
j=next[0]=-1;
for(i=0;i<lb;)
{
if(j==-1||b[i]==b[j])
next[++i]=++j;
else
j=next[j];
}
}
bool kmp()
{
getn();
int i=0,j=0;
for(i=0;i<la;)
{
if(j==-1||a[i]==b[j])
{
i++;
j++;
}
else
j=next[j];
if(j>=lb)
return 1;
}
if(j>=lb)
return 1;
return 0;
}
int main() {
int T;
cin>>T;
while(T--)
{
cin>>a>>b;
la=strlen(a);
lb=strlen(b);
int i,j;
int flag=1;
for(i=0;i<lb;i++)
{
if(b[i]=='0')continue;
flag=0;break;
}
for(i=lb-1;i>=0;i--)
if(b[i]=='0')b[i]='\0';
else break;
if(flag==1||kmp())
{
cout<<"Alice"<<endl;
}
else {
reverse(b,b+lb);
if(kmp())
cout<<"Alice"<<endl;
else cout<<"Bob"<<endl;
}
}
return 0;
}

本文介绍了一个游戏中的胜负预测算法,通过分析玩家的操作——翻转和除以十等行动,使用KMP算法判断是否可以在有限步骤内使两个玩家的数字相等,从而决定胜负。


被折叠的 条评论
为什么被折叠?



