0

The input consists of a string followed by N. The string consists of at most 30 uppercase characters, and N≤1018. The code outputs the Nth character of the infinite code built from the initial string. The first character is N=1.

The last character of the string is moved to the first position and then the resulting string is appended to the previous string in each iteration.

SAMPLE INPUT:

COW 8

SAMPLE OUTPUT:

C

In this example, the initial string COW expands as follows:

COW -> COWWCO -> COWWCOOCOWWC

                 12345678

The problem is that for large values of N eg.107, the code crashes. Can anyone please suggest methods to remove the error.

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

char sCowCode(string str, long long int N){

    long long int a = str.length();

    if(N < a){
        return str[N-1];
    }

    while(2*a < N + 1)
        a *= 2;

    if(a == N)
        return sCowCode(str, a-1);

    return sCowCode(str, N - a -1);
}



int main(){

    long long int N;
    string str;

    cin >> str;
    cin >> N;


    char a = sCowCode(str, N);

    cout<<a<<endl;
    cin.get();
    cin.get();

    return 0;
}
  • Does the string contain whitespaces? In that case `cin >> str;` doesn't work – Thomas Sablik Apr 02 '20 at 14:10
  • No, it does not contain any white spaces –  Apr 02 '20 at 14:11
  • 3
    What "error" do you get? My guess is that your code simply crashes because of too deep recursion. – Some programmer dude Apr 02 '20 at 14:13
  • @JohnFilleau Sample Input and Output provided. –  Apr 02 '20 at 14:17
  • 4
    On an unrelated note, please read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Some programmer dude Apr 02 '20 at 14:17
  • 1
    You'll probably need to transform your recursive algorithm into a looping algorithm. – François Andrieux Apr 02 '20 at 14:20
  • @FredLarson This code doesn't appear to be building any large strings. – aschepler Apr 02 '20 at 14:20
  • @Someprogrammerdude I know the problems with using namespac std and . But I am currently practicing for a programming competition where I would want to save as much time writing dependencies as much I can. –  Apr 02 '20 at 14:21
  • I don't understand the transition `COWWCO -> COWWCOOCOWWC` in your example. How did the second half, `OCOWWC`, come about? – Igor Tandetnik Apr 02 '20 at 14:22
  • @aschepler: Oh, I misunderstood. I thought N was the string size. – Fred Larson Apr 02 '20 at 14:23
  • @IgorTandetnik The string is rotated right, then appended to itself. Then THAT string is rotated right and appended to itself. Took me a minute, too. – JohnFilleau Apr 02 '20 at 14:23
  • 2
    @ShubhamRaj -- *I am currently practicing for a programming competiton…* -- Note the forum you are posting to. There is no competition here. Post clear, readable, non-obfuscated code. You are not racing someone when posting code here. – PaulMcKenzie Apr 02 '20 at 14:24
  • 1
    Any writing of code builds habits. And if you do a lot of online judge/competition site work you use a lot of bad habits. And like any habit, good as bad, it tend to stick. So please use good habits from the very beginning. It doesn't save that much time to use bad habits in the long run. And good habits will be better in the long run, especially when you want to apply for work. And more importantly, don't use such sites as a learning resource, because they're *not*. – Some programmer dude Apr 02 '20 at 14:28
  • @PaulMcKenzie Sorry for the bad code format. I am new to this forum and C++ as a whole so might take time getting used to. –  Apr 02 '20 at 14:29
  • @IgorTandetnik I edited the question to explain how the iteration works. –  Apr 02 '20 at 14:31
  • 1
    `sCowCode` is a perfect candidate for tail call optimization, but who knows exactly how those online judge sites compile? I agree with @FrançoisAndrieux: avoid recursion. – aschepler Apr 02 '20 at 14:31
  • 2
    @ShubhamRaj -- Ok. No problem. However when posting on SO, it would be good if 1) You have a local compiler to test with instead of the online compiler, 2) You take the time to format the code in a presentable manner -- again, you're not racing here 3) Place known input directly in the program code instead of `cin` or `scanf` statements. – PaulMcKenzie Apr 02 '20 at 14:32
  • 1
    Your recursive function is actually already close to tail recursive (what should make a rewriting as loop quite easier). For this, I would rewrite the last lines as `return sCowCode(str, N == a ? a - 1 : N - a -1);`. With a bit luck, the compiler recognizes/optimizes the tail-recursion on its own and you're done. – Scheff's Cat Apr 02 '20 at 14:33
  • 1
    That I didn't see it earlier! You never change `str` but you pass it by value. Change this to a `const` reference: `sCowCode(const string &str, long long int N)`. With this change, g++ 9.3 will compile the recursive calls as loop - and even without changing the last lines as recommended in my previous comment. [**Demo on Compiler Explorer**](https://gcc.godbolt.org/z/PKXFV2) FYI: [Tail call](https://en.wikipedia.org/wiki/Tail_call) – Scheff's Cat Apr 02 '20 at 14:51
  • @Scheff That worked, thanks. And thanks for the help to everyone. –  Apr 02 '20 at 14:58

0 Answers0