0

my code

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

int main() {

    int n ;
    cin >> n;


    string s1 , s2 , s3;
    cin >> s1 >> s2;

    string s = s1 + s1;

    int count = 0;

    for (int i = 0 ; s3 != s2 ; i++) {

        // int a = s.find()

        s3 = s.substr( i , s1.length() );

        count++;
    }

    cout << count - 1;

    return 0;
}

In this when I try to rotate the small sized string it is working fine but when I tried with some large length string it is showing error

terminate called after throwing an instance of 'std::out_of_range'
what():  basic_string::substr: __pos (which is 227) > this->size() (which is 226)

I'm not understanding what is happening as with the string length of 20 or 30 it is working fine but when then string length is over the 110 it is showing this type of error.

Also this testcase is showing error

113 ndafmffmuuwjzqpquwjhuftohawpfegsjvnxwipwqlswvawogjuyiqtzsgpwgosegmuuhpzwchejuiitumyescxxyecnsatcbfpseqzowvdjyvchg zqpquwjhuftohawpfegsjvnxwipwqlswvawogjuyiqtzsgpwgosegmuuhpzwchejuiitumyescxxyecnsatcbfpseqzowvdjyvchgavqnonmkwgqp

Chaitanya
  • 15
  • 4
  • 2
    As for me then I do not understand what this code is doing.:) In my opinion it does not make a sense. – Vlad from Moscow Jan 13 '22 at 09:12
  • 1
    You're calling `substr` with an index above the length of the string (227 in this case, while the string only has length 226) - so `i` in your for loop managed to count up to 227, and because this is 1 larger than the length of the string, you get a `std::out_of_range` exception [like documented](https://www.cplusplus.com/reference/string/string/substr/). – Turtlefight Jan 13 '22 at 09:13
  • What is the program supposed to do? Please show some simple working and non working examples of input and expected output vs. actual output. – Jabberwocky Jan 13 '22 at 09:17
  • 1
    Why do you assume that `for (int i = 0 ; s3 != s2 ; i++) {` is guaranteed to terminate? (That there is the case where `s2` is equal to `s3`?) `s` initialized with `s1 + s1` so there is no reason why `s2` and `s3` should have something in common? – t.niese Jan 13 '22 at 09:24
  • 1
    The condition will never become false in your problematic test case.(It looks like this could be solved with `s.find(s2)` and no loop.) – molbdnilo Jan 13 '22 at 09:27
  • [Why should I not `#include `?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) [Why is `using namespace std;` considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Evg Jan 13 '22 at 09:39
  • @Jabberwocky My question is this You are given a string and a number of queries to perform. For each query you have to rotate the string either left or right. For example, if the string is abcde, rotating left by 1 character will give bcdea. Now rotating again right by 1 character will give abcde back. Find the resultant string after performing all the queries. – Chaitanya Jan 13 '22 at 18:31
  • @Chaitanya please **[edit]** your question and put all relevant information into the question. – Jabberwocky Jan 13 '22 at 19:43

1 Answers1

1

This is quite simple your for loop has condition which is never false for this input. So you are increasing i beyond size of s (double of size of s1).

Note common part of this two strings is: zqpquwjhuftohawpfegsjvnxwipwqlswvawogjuyiqtzsgpwgosegmuuhpzwchejuiitumyescxxyecnsatcbfpseqzowvdjyvchg. Difference is:

  • s1 has in front: ndafmffmuuwj
  • s2 has in backL: avqnonmkwgqp

So s1 is not rotation of s2 that is why codition in for is never fasle.

This is the reason std::basic_string::substr throws an exception.

Live demo.
Some fix.

And here is better way to do it.

Marek R
  • 32,568
  • 6
  • 55
  • 140