Monday, 6 August 2012

UVa 10324 - Zeros and Ones Solution

#include<iostream>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<set>
#include<utility>
#include<iomanip>
#include<queue>

using namespace std;

#define INF (1<<29)
#define SET(a) memset(a,-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define FILL(a,v) memset(a,v,sizeof(a))
#define PB push_back
#define FOR(i,n) for(int i = 0;i<n;i++)
#define PI acos(-1.0)
#define EPS 1e-9
#define MP(a,b) make_pair(a,b)
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define READ freopen("input.txt", "r", stdin)
#define WRITE freopen("output.txt", "w", stdout)
#define LL long long
#define MX 1000010
#define MOD 1000000007

int arr[MX];
char s[MX];
main()
{
    int kk=1;
    while(gets(s))
    {
        int sl=strlen(s);
        CLR(arr);
        arr[0]=0;
        for(int i=1;i<sl;i++)
            if(s[i]==s[i-1])
                arr[i]=arr[i-1];
            else
                arr[i]=arr[i-1]+1;

        printf("Case %d:\n",kk++);

        int mx,mn,q;
        scanf("%d",&q);
        while(q--)
        {
            scanf("%d%d",&mn,&mx);
            if(mn>mx)
                swap(mn,mx);

            if(s[mn]==s[mx] && (arr[mx]-arr[mn])==0)
                printf("Yes\n");
            else printf("No\n");
        }
        //cout<<"done"<<endl;
        getchar();
    }
return 0;
}

1 comment:

  1. can u explain to me a bit of ur algorithm? wat is int arr[] is used for. and why it does speed up the computation. I really like to learn please help me. Thanks

    ReplyDelete