Solution of 10324 - Zeros and Ones

Problem Description
source:https://uva.onlinejudge.org/external/103/10324.html

Given a string of 0’s and 1’s up to 1000000 characters long and indices i and j, you are to answer a question whether all characters between position min(i, j) and position max(i, j) (inclusive) are the same. 

Input

There are multiple cases on input. The first line of each case gives a string of 0’s and 1’s. The next line contains a positive integer n giving the number of queries for this case. The next n lines contain queries, one per line. Each query is given by two non-negative integers, i and j. For each query, you are to print ‘Yes’ if all characters in the string between position min(i, j) and position max(i, j) are the same, and ‘No’ otherwise. 


Output 

Each case on output should start with a heading as in the sample below. The input ends with an empty string that is a line containing only the new line character, this string should not be processed. The input may also with end of file. So keep check for both.

Sample Input

0000011111
3
 0 5
4 2
5 9
01010101010101010101010101111111111111111111111111111111111110000000000000000
5
4 4
25 60
1 3
62 76
24 62
1
1
0 0

Sample Output 

Case 1:
No
Yes
Yes
Case 2:
Yes
Yes
No
Yes
No
Case 3:
Yes

Solution
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <string>
#include <vector>
#include<stdio.h>
#include<stdlib.h>

using namespace std;
int main()
{
    register long int i,j,n,a,b,ma,mi,get,c=0;
    char input[1000005];
    //freopen("in.txt","r",stdin);
    while(scanf("%s",input)==1)
    {
        cin>>n;
        c+=1;
        printf("Case %ld:\n",c);
        for(i=1;i<=n;i++)
        {
            cin>>a>>b;
            ma=max(a,b);
            mi=min(a,b);
            get=0;
            for(j=mi+1;j<=ma;j++)
            {
                if(input[j]==input[j-1])
                {
                    continue;
                }
                else
                {
                    get=1;
                    break;
                }
            }
            if(get==1)
                printf("No\n");
            else
                printf("Yes\n");
        }
        memset(input,'\0',sizeof(input));
    }
    return 0;
}
image

No comments:

Post a Comment

Write your comment - Share Knowledge and Experience