zoukankan      html  css  js  c++  java
  • 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
    ");
                continue;
            }
            if(kmp(s,t)||!strcmp(t,"0"))
            {
                printf("Alice
    ");
                continue;
            }
            reverse(t,t+lt);
            if(kmp(s,t))
            {
                printf("Alice
    ");
                continue;
            }
            printf("Bob
    ");
        }
        return 0;
    }
    

      

  • 相关阅读:
    Oracle创建自增字段方法-ORACLE SEQUENCE的简介
    iOS项目开发实战——使用Xcode6设计自己定义控件与图形
    准备开源用javascript写Tomcat下的WebApp的项目
    Codeforces Round #256 (Div. 2) B. Suffix Structures
    静默方式安装10g数据库软件+升级patch+手工建库
    oracle 数据库开发面试题
    待机异常篇
    HTTP状态码(HTTP Status Code)
    POJ3126——Prime Path
    RHEL7 -- 通过gerp使用正则表达式
  • 原文地址:https://www.cnblogs.com/onlyli/p/7225295.html
Copyright © 2011-2022 走看看