You are not logged in. Please login at www.codechef.com to post your questions!

×

Why isn't this code compiling?

Please help in finding what's wrong with this piece of code

#include< bits/stdc++.h >
using namespace std;

map< string,int > mp;
map< string,int > present;

int minChars(string& s,int i,int j)
{
    if(i>=j)
        return 0;
    if(s[i]==s[j])
        return minChars(s,i+1,j-1);
    else
    {
        if(present[s])
            return mp[s];
        else
        {
            string a=s[j]+s.substr(i,j-i+1);
            string b=s.substr(i,j-i+1)+s[i];
            int p,q,r,s;
            p=r=0;
            q=a.length()-1;
            s=b.length()-1;
            mp[s]=min(1+minChars(a,p,q),1+minChars(b,r,s));
            present[s]=1;
            return mp[s];
        }       
    }
}
int main()
{   
    int t;
    cin>>t;
    string s;
    while(t--)
    {
        cin>>s;
        int k=minChars(s,0,s.length()-1);
        cout<< k;
        cout<< endl;
    }
    return 0;
}

asked 30 Jan '18, 14:46

ramini's gravatar image

2★ramini
615
accept rate: 8%


There are 2 errors (so it seems :D) :

  • It seems that we can't use whitespace characters inside $<$ and $>$.
  • There's another integer variable with the same name (s) as that of the function parameter. Precisely, in line int p, q, r, s declares an integer s and then the string s is shadowed here.
link

answered 30 Jan '18, 15:43

shubhambhattar's gravatar image

3★shubhambhattar
2888
accept rate: 23%

edited 30 Jan '18, 15:47

Thanks a ton bro, silly mistake I'll take care of variable names from next time !!!

(30 Jan '18, 19:17) ramini2★

Glad to be of help :)

(31 Jan '18, 12:25) shubhambhattar3★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×16
×3

question asked: 30 Jan '18, 14:46

question was seen: 157 times

last updated: 31 Jan '18, 12:25