Find Your Gift | CodeChef

`#include<stdio.h>
#include<string.h>
int main()
{
int t;
scanf(“%d”,&t);
for(int i=0;i<t;i++)
{
int n;
int x=0;
int y=0;
scanf(“%d”,&n);
char s[1000];
scanf(“%s”,s);
for(int j=0;j<n;j++)
{
if(s[j]==‘L’||s[j]==‘R’)
{
for(int k=j+1;k<n;k++)
{
if(s[k]==‘L’||s[k]==‘R’)
{
s[k]=‘Y’;
}
if(s[k+1]==‘U’||s[k+1]==‘D’)
{
break;
}

            }
        }
        if(s[j]=='U'||s[j]=='D')
        {
            for(int k=j+1;k<n;k++)
            {
                if(s[k]=='U'||s[k]=='D')
                {
                    s[k]='Y';
                }
                if(s[k+1]=='R'||s[k+1]=='L')
                {
                    break;
                }

            }
        }

    }

    for(int j=0;j<n;j++)
    {
        if(s[j]=='L')
        {
            x=x-1;
        }
        if(s[j]=='R')
        {
            x=x+1;
        }
         if(s[j]=='U')
        {
            y=y+1;
        }
        if(s[j]=='D')
        {
            y=y-1;
        }

    }
    printf("%d %d\n",x,y);

}

}`
this submission of mine is showing WA
i dont know what to do
plz help

https://www.codechef.com/viewsolution/30671114
Please help what is wrong here?

Please give a link to solution that would be easier for the other person.

1 Like

ok

This is my solution

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        char str[n];
        cin>>str;
        char bkp = 'I';
        pair<int,int> p(0,0);
        for(int i=0;i<n;i++)
        {
            if (str[i] == 'L' && bkp != 'L' && bkp != 'R')
                p.first -= 1;
            else if(str[i] == 'R' && bkp != 'R' && bkp != 'L')
                p.first += 1;
            else if(str[i] == 'U' && bkp != 'U' && bkp != 'D')
                p.second += 1;
            else if(str[i] == 'D' && bkp != 'D' && bkp != 'U')
                p.second -= 1;
            bkp = str[i]; 
        }
        cout<<p.first<<" "<<p.second<<"\n";
    }
    return 0;
}

just simple run the program it will show you the complilation error ;

One basic thing i found is

          {
               if(ch[c]=='L')
          {
             a=a-1; 
          }
          else if(ch[c]=='R')
          {
              a=a+1;
          }
          else if(ch[c]=='U')
          {
              b=b+1;
          }}
here you haven't consider the case  ch[c] == 'D'

One basic thing i found is

          if(c==0)
          {
               if(ch[c]=='L')
          {
             a=a-1; 
          }
          else if(ch[c]=='R')
          {
              a=a+1;
          }
          else if(ch[c]=='U')
          {
              b=b+1;
          }}
here you haven't consider the case  ch[c] == 'D'

thank you so much.