KMP——Game

Problem 2275 Game

Accept: 41    Submit: 109
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

4
11111 1
1 11111
12345 54321
123 123

Sample Output

Alice
Bob
Alice
Alice

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
char s[1000005],t[1000005],tmp[1000005];
int nextl[1000005];
int ls,lt;
void getnext()
{
    nextl[0]=-1;
    for(int i=1;i<lt;i++)
    {
        int j=nextl[i-1];
        while(t[j+1]!=t[i]&&j>-1)
            j=nextl[j];
        nextl[i]=(t[j+1]==t[i])?j+1:-1;
    }
}
int kmp(char *a,char *b)
{
    getnext();
    int sum=0,i=0,j=0;
    while(i<ls&&j<lt)
    {
        if(j==-1||a[i]==b[j])
            i++,j++;
        else
            j=nextl[j];
    }
    if(j==lt)
        return 1;
    return 0;
}
int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        cin>>s>>t;
        ls=strlen(s),lt=strlen(t);
        if(ls<lt)
        {
            printf("Bob\n");
            continue;
        }
        if(kmp(s,t)||!strcmp(t,"0"))
        {
            printf("Alice\n");
            continue;
        }
        reverse(t,t+lt);
        if(kmp(s,t))
        {
            printf("Alice\n");
            continue;
        }
        printf("Bob\n");
    }
    return 0;
}

  

时间: 2024-08-27 10:36:40

KMP——Game的相关文章

Educational Codeforces Round 21 G. Anthem of Berland(dp+kmp)

题目链接:Educational Codeforces Round 21 G. Anthem of Berland 题意: 给你两个字符串,第一个字符串包含问号,问号可以变成任意字符串. 问你第一个字符串最多包含多少个第二个字符串. 题解: 考虑dp[i][j],表示当前考虑到第一个串的第i位,已经匹配到第二个字符串的第j位. 这样的话复杂度为26*n*m*O(fail). fail可以用kmp进行预处理,将26个字母全部处理出来,这样复杂度就变成了26*n*m. 状态转移看代码(就是一个kmp

hiho 1015 KMP算法 &amp;&amp; CF 625 B. War of the Corporations

#1015 : KMP算法 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进. 这一天,他们遇到了一只河蟹,于是河蟹就向小Hi和小Ho提出了那个经典的问题:“小Hi和小Ho,你们能不能够判断一段文字(原串)里面是不是存在那么一些……特殊……的文字(模式串)?” 小Hi和小Ho仔细思考了一下,觉得只能想到很简单的做法,但是又觉得既然河蟹先生这么说了,就

hdu2328 Corporate Identity 扩展KMP

Beside other services, ACM helps companies to clearly state their "corporate identity", which includes company logo but also other signs, like trademarks. One of such companies is Internet Building Masters (IBM), which has recently asked ACM for

KMP算法详解

这几天学习kmp算法,解决字符串的匹配问题,开始的时候都是用到BF算法,(BF(Brute Force)算法是普通的模式匹配算法,BF算法的思想就是将目标串S的第一个字符与模式串T的第一个字符进行匹配,若相等,则继续比较S的第二个字符和 T的第二个字符;若不相等,则比较S的第二个字符和T的第一个字符,依次比较下去,直到得出最后的匹配结果.BF算法是一种蛮力算法.)虽然也能解决一些问题,但是这是常规思路,在内存大,数据量小,时间长的情况下,还能解决一些问题,但是如果遇到一些限制时间和内存的字符串问

2016——3——16 kmp习题

1.传送门:http://begin.lydsy.com/JudgeOnline/problem.php?id=2725 题目大意:找一个串在另一个串中出现的次数 题解:kmp(纯裸题) 2.传送门:http://begin.lydsy.com/JudgeOnline/problem.php?id=2732 题目大意:给你一个字符串,让你求出最大重复周期(最大周期不和本身重合) 题解:kmp或者扩展kmp(但我并未用这种方法),我用的是kmp,但是一直WA,原来是求next数组把while写成了

KMP算法

1 /* next数组是KMP算法的关键,next数组的作用是:当模式串T和主串S失配 2 * ,next数组对应的元素指导应该用T串中的哪一个元素进行下一轮的匹配 3 * next数组和T串相关,和S串无关.KMP的关键是next数组的求法. 4 * 5 * ——————————————————————————————————————————————————————————————————— 6 * | T | 9 | a | b | a | b | a | a | a | b | a | 7

KMP

http://www.matrix67.com/blog/archives/115 http://www.cnblogs.com/c-cloud/p/3224788.html 1 #include<stdio.h> 2 #include<algorithm> 3 #include<iostream> 4 #include<string.h> 5 #include<stdlib.h> 6 #include<math.h> 7 #incl

朴素和KMP模式匹配算法(Java)

朴素模式匹配算法 public class Test { //朴素模式匹配算法 public int Index(String s,String t,int pos){ int i = pos;//主串中第几个位置开始比较 int j = 0;//模式串中的第一个位置 while(i<s.length()&&j<t.length()){ if(s.charAt(i)==t.charAt(j)){ i++; j++; }else { i = i-j+1;//主串的下一个位置 j

扩展KMP算法

扩展KMP,用于求s的后缀的最长前缀.用extand数组表示第i个后缀的最长前缀的字符个数. 注意几点:1.next数组是对T的   2.extand数组是对S的 3.应用:回文,重复串等 代码如下: 1 #include<iostream> 2 #include<string> 3 #include<cstdio> 4 using namespace std; 5 const int MM=100005; //长度最大值 6 int next[MM],extand[M

KMP算法解决字符串出现次数

比如主串为:"1001110110" 子串为:"11" 则出现位置分别为:3 4 7 //KMP算法 2015.6.7 #include<iostream> #include<stdlib.h> using namespace std; int main() { char *s = "1001110110"; char *p = "11"; int ar[20] = { 0 }; //next ar[0