Canna

Saturday, 29 September 2018

Play With Strings

  • Problem Description

    Given are two strings(each not more than 100 characters). We need to find relation between them such that all the characters of the second string must appear in the first string sequentially (not necessarily continuously). The program should :

    Print 1: if the characters in the first string appear in the same sequence as that of the second string.
    Print 2: if the characters in the first string appear in the opposite sequence as that of second string.
    Print 3: if the characters in the first string appear in both ie same as well as opposite sequences of the second string.
    Print 0: if the characters in the first string dont appear in sequence of the second string. 


    The characters in the strings are case-sensitive.
    Input

    The first line should give the number of test cases. For each test case, it takes input two strings.
    Output

    An integer ( 0,1,2 or 3 ) for each of the test cases. 
  • Test Case 1

    Input (stdin)
    2
    
    njack njk
    
    anwesha awesa
    
    
    Expected Output
    1
    
    1
  • Test Case 2

    Input (stdin)
    2
    
    wysiwig iwi
    
    abcdef edba
    
    
    Expected Output
    3
    
    2
  • Program
  • #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        //printf("Hello world!\n");
        int t,i,j,ans;
        char s1[102],s2[102];
        scanf("%d",&t);
        while(t--)
        {
            scanf("%s%s",s1,s2);
            for(j=-1,ans=i=0;s2[i];i++)
            {
                for(j++;s1[j];j++)
                 if(s2[i]==s1[j])
    
                   break;
    
                if(j>=strlen(s1))
                   break;
            }
    
            if(i==strlen(s2))
              ans++;
            for(j=-1,i=strlen(s2)-1;i>=0;i--)
            {
                for(j++;s1[j];j++)
                 if(s2[i]==s1[j])
                   break;
                if(j>=strlen(s1))
                   break;
            }
            if(i==-1)
            {
              if(ans)
                ans=3;
              else
                ans=2;
            }
            printf("%d\n",ans);
        }
        return 0;
    }

No comments:

Post a Comment

Note: only a member of this blog may post a comment.