Cpp string behaving strangely!(solved)

c-plus-plus
output
string

#1

My code is printing different outputs when i print something additionally . If i print the line 15 (mentioned in my code) I get a different output , whereas if I do not print that line I get a different final output . Can you please help me know why ?
Here is the question link . question
My code is below . Any help will be really appreciated .

/*this is the code */

#include<bits/stdc++.h>
using namespace std;
typedef long ll ;
int main()
{
string s1,s2;
getline(cin,s1);
getline(cin,s2);
ll f1[26]={0};
ll f2[26]={0};
ll n = s1.length();
ll l2 = s2.length();
cout<<l2<<endl;          


/* printing the above line chages final answer. why???  */
for(ll i = 0 ; i<l2 ; i++)
{
    if(s2[i]!=' ')
    f2[s2[i]-97]++;
}
ll l=-1,r=-1,ans=-1,lans=99999,count = 0;
while(l<n && r<n)
{
    if(count==l2)
    {
        if(l<n && l>=0)
        {
            int len = r-l+1;
            if(len<lans)
            {
                lans = len;
                ans = l;
            }
            if(f2[s1[l]-97]>0)
            {
                f1[s1[l]-97]--;
                if(f1[s1[l]-97]<f2[s1[l]-97])
                count--;
            }
        }
        l++;
    }
    else
    {
        r++;
        if(r<n && r>=0)
        {
            if(f2[s1[r]-97]>0)
            {
                f1[s1[r]-97]++;
                if(f1[s1[r]-97]<=f2[s1[r]-97])
                count++;
            }
        }
    }
}
string ss = s1.substr(ans , lans);
cout<<ss<<endl;
}

LINK = compiler link .
This is a link to my compiled code please refer to this .


#2

Wow . I got the answer for this strange behaviour .
Instead of using

typedef long ll;

we use

typedef int ll;

and everything works just fine . This is strange to me .
Would be great if anyone could help clear this or lead to a link to clear this confusion .


#3

If we replace endl with β€œ\n” in line 15, output remains same as commenting that line . So basically the answer is difference between endl and β€œ\n”, but I can’t figure out any further!


#4

Wow . This is even more confusing now . Well , my above comment explains that I was refering to the string index using long instead of int data type so I thought there was the error . But different output using \n and endl leads to a completely new discussion here .


#5

You could put this problem on stackoverflow. They might have a solution.


#6

Yes . But I do not have an id there . Will surely go for other platforms like stackoverflow if I do not get any help here on codechef . But I guess there would be many people on codechef facing errors like this in their daily coding . Hope they come to rescue .


#7

If you run this on codechef.com/ide, this will give the same solution in both the cases.
One reason that I am aware of is, that things like fflush are completely compiler dependent, so this strange behaviour is not reproducible.


#8

Sorry but codechef ide also gives 2 different answers .
And i do not think that fflush has anything to do with it because with or without them , the problem still exists .


#9

OK . Now i have found the answer finally by careful observation .
The only thing that is causing problem here is that I am using statements like
f1[s1[r]-97];
and accessing other array indexes without checking if s1[r]-97 is non negative .
It is negative when s1[r] is a space .
So just checking this condition if (s1[r]-97>=0) and then accessing array index solves all the complexities .
Thank you to everyone for their time to help me out :slight_smile:


#10
  1. Changing f1 and f2’s datatypes to int corrects the bug.
  2. Also removing endl from the line, gives the same output. What I have figured is the bug has something to do with endl.
    If we use cout<<endl; i.e omitting printing l2 gives different string than commenting it.
    And its not about the location of endl, anywhere before the while loop, gives a bugged output. See here
    for more, I have added a cout<<endl in the loop itself on line 28. A whole new output is coming in this case.
    Not sure why its behaving this way !

#11

All this undefined behaviour because i was accessing array indices with negative values .
I was accessing f1[s1[r]-97] without making sure if (s1[r]-97 is >=0) .(which will be negative if s1[r] is a space character . ASCII val = 32)
Adding the above condition before accessing array index solves all the problem of using endl or β€œ\n” or int or long .
Thanks for your time though .Really appreciate it @shivamshivi99