Game (子串问题 模板)

来源:互联网 发布:大排畸哪些数据看男女 编辑:程序博客网 时间:2024/06/03 14:01

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

 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简化为A,Bob简化为B,A,B各有2个属于他们自己的数字,通过2种方法 1.数字串翻转,2.数字串/10,且仍为整数,A先B后,判断A是否可以与B相同,如果可以A 赢 否则B赢。

思路:判断是否为子串,如果是倒叙的子串也可以。

附上水过数据弱的代码:

#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;char a[1000005],b[1000005];int suma[15],sumb[15];int main(){    int icase;    scanf("%d",&icase);    while(icase--)    {       scanf("%s%s",a,b);       int la=strlen(a),lb=strlen(b),flag=1;       memset(suma,0,sizeof(suma));       memset(sumb,0,sizeof(sumb));       if(la>=lb&&flag)       {           for(int i=0;i<la;i++)             suma[a[i]-'0']++;          for(int i=0;i<lb;i++)             sumb[b[i]-'0']++;         for(int i=0;i<=9;i++){           if(suma[i]<sumb[i]){               flag=0;               break;           }         }         if(sumb[0]>0)         {           for(int i=1;i<=9;i++){             if(sumb[i]==0){                flag=1;                break;             }          }         }       }       else        flag=0;       if(flag)         printf("Alice\n");       else         printf("Bob\n");  }    return 0;}


由于是子串问题可以使用数据结构种的BF算法,KMP算法

BF算法版本:(BF知识储备:http://blog.csdn.net/deepseazbw/article/details/75896335)

#include <iostream>#include <cstring>#include <cstdio>using namespace std;char a[1000005],b[1000005];int main(){    int icase;    scanf("%d",&icase);    while(icase--)    {        scanf("%s%s",a,b);        int len1=strlen(a),len2=strlen(b),flag=0;        if(len1>=len2){          for(int j=0,i=0;j<len2&&i<len1;){            if(a[i]==b[j])  {                 i++;                 j++;            }            else            {               i=i-j+1;               j=0;            }            if(j==len2)            {                flag=1;                break;            }        }        if(!flag)        {           char c[1000005];           for(int i=0,j=len2-1;i<len2;i++,j--)               c[i]=b[j];            //printf("%s\n",c);           for(int j=0,i=0;j<len2&&i<len1;){            if(a[i]==c[j])  {                 i++;                 j++;            }            else            {               i=i-j+1;               j=0;            }            if(j==len2)            {                flag=1;                break;            }         }       }       if(!flag)       {           if(strcmp(b,"0")==0)              flag=1;       }      }if(flag)            printf("Alice\n");        else            printf("Bob\n");   }    return 0;}

KMP算法版本(KMP知识储备)

1.http://www.cnblogs.com/c-cloud/p/3224788.html  

2.http://www.61mon.com/index.php/archives/183/  

3.http://www.cnblogs.com/SYCstudio/p/7194315.html


#include<cstdio>#include<cstring>#include<string>#include<queue>#include<set>#include<vector>#include<iostream>using namespace std;const int N=100001;string a,b;int la,lb,flag=0;int ne[N+1];char c[N+1];void Ne(int len,int *ne){    ne[0]=-1;    int i=-1,j=0;    while(j<len)    {        if(i==-1||b[i]==b[j])        {            i++;            j++;            if(b[i]!=b[j])                ne[j]=i;            else                ne[j]=ne[i];        }        else            i=ne[i];    }}void KMP(){    int len1=la,len2=lb;    Ne(len2,ne);    int i=0,j=0;    flag=0;    //printf("%s\n",c);    while(i<len1)    {        if(j==-1||a[i]==b[j])        {            i++;            j++;        }        else            j=ne[j];        if(j==len2)        {            flag=1;            return ;            j=ne[j];        }    }}void KMP2(){    int len1=la,len2=lb;    Ne(len2,ne);    int i=0,j=0;    flag=0;    //printf("%s\n",c);    while(i<len1)    {        if(j==-1||a[i]==c[j])        {            i++;            j++;        }        else            j=ne[j];        if(j==len2)        {            flag=1;            return ;            j=ne[j];        }    }}int main(){    int t;    scanf("%d",&t);    while(t--)    {        //getchar();        //scanf("%s%s",a,b);        cin>>a>>b;        la=a.size(),lb=b.size();        if(la<lb) puts("Bob");        else        {            KMP();            if(flag)                puts("Alice");            else            {                int i,j;                for(i=lb-1,j=0; i>=0; i--,j++)                {                    c[j]=b[i];                }                //c[j]='\0';                //b.reverse();                //cout<<c<<endl;                KMP2();                if(flag)                    puts("Alice");                else                {                    if(lb==1&&b[0]=='0')                    puts("Alice");                    else puts("Bob");                }            }        }    }    return 0;}
再附上 <<挑战程序设计竞赛>>上的P3734.7.2字符串匹配中哈希算法用于字符串匹配的Rabin-Karp算法,比KMP更加简洁,复杂度也相差无几。但是严格意义上来说正确性没那么高。

#include <iostream>#include <cstdio>#include <string>#include <algorithm>using namespace std;typedef unsigned long long ull;ull B = 100000007;//b是否包含a,或者说a有没有在b中出现过bool contain(string a,string b){    int al=a.length(),bl=b.length();    if(al>bl) return false;       ull t=1;    for(int i=0;i<al;i++) t*=B;       ull ah=0,bh=0;    for(int i=0;i<al;i++) ah = ah*B+a[i];    for(int i=0;i<al;i++) bh = bh*B+b[i];       for(int i=0;i+al<=bl;i++)    {        if(ah==bh)  return true;        if(i+al<bl) bh = bh*B+b[i+al]-b[i]*t;    }    return false;}bool allZero(string &an){    for(int i=0;an[i]!='\0';i++)        if(an[i]!='0')            return false;    return true;}int main(int argc,char *argv[]){   //小技巧:解除IOS输入输出流和STDIO的同步    //可以加快cin读入的速度    std::ios::sync_with_stdio(false);    int T;    scanf("%d",&T);    string a,b;    while(T--)    {        cin>>a>>b;        //如果b在a中出现过,或者b字符串都是0        if(contain(b,a)||allZero(b))            printf("Alice\n");        else        {            reverse(a.begin(),a.end());//翻转字符串            if(contain(b,a))                printf("Alice\n");            else                printf("Bob\n");        }    }    return 0;}




原创粉丝点击