Poj Solution 2589

http://poj.org/problem?id=2589

#include<iostream>
#include"string.h"
#include"stdlib.h"
using namespace std;
int main()
{
char a[110],b[110],ar[110],br[110];
int n1,r1,n2,r2,k=0,i,g1,g2,r;


cin>>a>>b;
n1=n2=strlen(a);
r1=0;r2=0;
g1=0;g2=0;

    while(k<1000)
    {if(g1==n1){if(r1==0){cout<<"John wins."<<endl;break;}for(i=r1-1;i>=0;i--)a[i]=ar[i];n1=r1;r1=0;g1=0;}
     if(g2==n2){if(r2==0){cout<<"Jane wins."<<endl;break;}for(i=r2-1;i>=0;i--)b[i]=br[i];n2=r2;r2=0;g2=0;}
     ar[r1++]=a[g1++];br[r2++]=b[g2++];
     if(ar[r1-1]==br[r2-1]) {r=rand()/99%2;
         if(r==0){for(i=0;i<r2;i++)ar[r1+i]=br[i];r1+=r2;r2=0;
        cout<<"Snap! for Jane: ";for(i=r1-1;i>=0;i--)cout<<ar[i];cout<<endl;
        }
            
     else {for(i=0;i<r1;i++)br[r2+i]=ar[i];r2+=r1;r1=0;
        cout<<"Snap! for John: ";for(i=r2-1;i>=0;i--)cout<<br[i];cout<<endl;}
     }
     k++;}
    
     if(k==1000)cout<<"Keeps going and going ..."<<endl;


return 1;
}
											
This entry was posted in poj. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *