Tuesday, 12 July 2016

Solution of URI 2126 :: Searching Subsequences


Before seeing the solution make sure that you tried enough. Don’t paste the whole code, just find out the logic. If you stuck in trouble, just inform me on comment.

/**Bismillahir Rahmanir Rahim.**/

#include <stdio.h>
#include <string.h>

int main()
{
    int i, j, cnt = 0, cnt2, k, l;
    char sb_str[100], str[100];
    while(scanf("%s%s", sb_str, str)!=EOF)
    {
        int total = 0, sve, tmp;
        int sb_lnth = strlen(sb_str), str_lnth = strlen(str);
        for(i=0, cnt++; i <= str_lnth-sb_lnth; i++)
        {
            if(str[i] == sb_str[0])
            {
                cnt2 = 1; tmp = i + 1;
                for(k = i+1, l = 1; sb_str[l]; l++, k++)
                {
                    if(str[k] == sb_str[l]) cnt2++;
                    else break;
                }
                if(cnt2 == sb_lnth)
                {
                    total++;
                    sve = tmp;
                }
            }
        }
        printf("Caso #%d:\n", cnt);
        if(total == 0) printf("Nao existe subsequencia\n\n");
        else
        {
            printf("Qtd.Subsequencias: %d\n", total);
            printf("Pos: %d\n\n", sve);
        }
    }
    return 0;
}

4 comments:

  1. Brother http://pastebin.com/c6A1y9GR
    this is my code. It's giving correct output on console but showing 100%WA at uri. I read my code so many times and then read yours too carefully. And I can't find where I've mistaken. Can you plz help me out from here?

    ReplyDelete
    Replies
    1. I did d silly mistake. solved it :)
      but it said "if there is more of a subsequence, print where it started the last substring". Doesn't it mean that if subsequences are more than one? I didn't get it.

      Delete
  2. need more solve of uri problems . still a lot problem needs to solve..

    ReplyDelete

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