foj Problem 2275 Game
Accept: 145 Submit: 844
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.
题意:两个人操作各自的字符串,如果Alice通过有限的操作能得到和Bob一样的字符串,那么Alice赢,否则Bob赢
思路:仔细想想可以发现如果Bob的字符串如果是Alice的子串,那么Alice一定能通过有限的操作最终得到和Bob一样的字符串。
KMP字符串匹配即可,Alice的字符串以及其反串都判断一遍,注意在判断反串之前把反串中的前导0去掉。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int Next[+] = { };
/* P为模式串,下标从0开始 */
void GetNext(string P, int next[])
{
int p_len = P.size();
int i = ; //P的下标
int j = -;
next[] = -; while (i < p_len)
{
if (j == - || P[i] == P[j])
{
i++;
j++;
next[i] = j;
}
else
j = next[j];
}
} /* 在S中找到P第一次出现的位置 */
int KMP(string S, string P, int next[]){
GetNext(P, next); int i = ; //S的下标
int j = ; //P的下标
int s_len = S.size();
int p_len = P.size(); while (i < s_len && j < p_len)
{
if (j == - || S[i] == P[j]) //P的第一个字符不匹配或S[i] == P[j]
{
i++;
j++;
}
else
j = next[j]; //当前字符匹配失败,进行跳转
} if (j == p_len) //匹配成功
return i - j; return -;
}
string s1, s2;
int main(){
int t;
scanf("%d",&t);
while (t--) {
cin >> s1 >> s2;
if (KMP(s1, s2, Next) != -) { printf("Alice\n"); continue; }
else {
string tmp; bool flag = ; int k;
reverse(s2.begin(), s2.end());
for (k = ; k < s2.size();k++) //去掉前导0
if (s2[k] != '')break;
tmp = s2.substr(k, s2.size());
if (KMP(s1, tmp, Next) != -)printf("Alice\n");
else printf("Bob\n");
}
} return ;
}
foj Problem 2275 Game的更多相关文章
- fzu Problem 2275 Game(kmp)
Problem 2275 Game Accept: 62 Submit: 165Time Limit: 1000 mSec Memory Limit : 262144 KB Proble ...
- FOJ ——Problem 1759 Super A^B mod C
Problem 1759 Super A^B mod C Accept: 1368 Submit: 4639Time Limit: 1000 mSec Memory Limit : 32 ...
- FOJ Problem 1016 无归之室
Problem 1016 无归之室 Accept: 926 Submit: 7502Time Limit: 1000 mSec Memory Limit : 32768 KB Prob ...
- FOJ Problem 1015 土地划分
Problem 1015 土地划分 Accept: 823 Submit: 1956Time Limit: 1000 mSec Memory Limit : 32768 KB Probl ...
- foj Problem 2107 Hua Rong Dao
Problem 2107 Hua Rong Dao Accept: 503 Submit: 1054Time Limit: 1000 mSec Memory Limit : 32768 K ...
- foj Problem 2282 Wand
Problem 2282 Wand Accept: 432 Submit: 1537Time Limit: 1000 mSec Memory Limit : 262144 KB Prob ...
- FOJ Problem 2273 Triangles
Problem 2273 Triangles Accept: 201 Submit: 661Time Limit: 1000 mSec Memory Limit : 262144 KB P ...
- foj Problem 2283 Tic-Tac-Toe
Prob ...
- FOJ Problem 2257 Saya的小熊饼干
...
随机推荐
- Bootstrap历练实例:带列表组的面板
带列表组的面板 我们可以在任何面板中包含列表组,通过在 <div> 元素中添加 .panel 和 .panel-default 类来创建面板,并在面板中添加列表组.您可以从 列表组 一章中 ...
- Java常用的一些容器
转自:https://www.cnblogs.com/LipeiNet/p/5888513.html 前言:在java开发中我们肯定会大量的使用集合,在这里我将总结常见的集合类,每个集合类的优点和缺点 ...
- mysql中的的按小数位截取
format()函数返回类型是字符串,满三位会加一个逗号. 针对数字类型转换建议使用 convert或者cast函数,用法如下: format(param, 2) (不建议) convert(para ...
- Linux基础学习-用户的创建修改删除
用户添加修改删除 1 useradd添加用户 添加一个新用户hehe,指定uid为3000,家目录为/home/haha [root@qdlinux ~]# useradd -u 3000 -d /h ...
- .Net Core依赖注入中TryAddEnumerable 和TryAddTransient方法的区别
.Net Core依赖注入添加的每个服务,最终都会转换为一个ServiceDescriptor的实例,ServiceDescriptor包含以下属性: Lifetime:服务的生命周期(Singlet ...
- VUE2.0声明周期钩子:不同阶段不同钩子的开启
- phpstorm 工具使用技巧(持续补充中。。。)
phpstorm 工具使用技巧(持续补充中...) 一.phpstorm大小写切换 1.选择要转换的目标字符串: //普通商家,普通折扣默认值'COMMON_DISCOUNT'=>10.00, ...
- CSS清除浮动8大方法
CSS清除浮动是每一位web前端工程师都要掌握的技术,也是让每一位刚入门的前端工程师感到头疼的问题, 下面就来讲一下CSS清除浮动的原理和各种解决方法,大家可以根据实际情况选择最佳的解决方案. 在用D ...
- 安装ipython的情况总结
在知乎上看待一位朋友的Python的学习记录,准备跟着学一下.这位朋友用了ipython(因为他主要做科学计算,我自己将来要学习数据科学,感觉很合适),酒准备安装一下ipython,没想到出了不少问题 ...
- request response cookie session
request 1. url传递参数 1)参数没有命名, 如: users/views def weather(request, city, year): print(city) print(year ...