50分求助优化!! 超时了.....

P3612 [USACO17JAN] Secret Cow Code S

sana_37 @ 2023-08-04 13:38:58

#include<iostream>
using namespace std;
#include<string.h>
void Reverse(string str, long long n, int len)
{
    if (n-1 < len&&n-1>=0)
    {
        cout << str[n-1] << endl;
        return;
    }
    int len1 = len;
    while (n > len1)
    {   
        len1 *= 2;
    }

    if (n ==len1/2+1)
    {
        Reverse(str, n - 1, len);
    }
    else
    {
        Reverse(str, n - 1 - len1/2, len);
    }
}

int main()
{
    string str;
    cin >> str;
    int len = str.length();
    long long n;

    cin >> n;
    Reverse(str, n, len);

    return 0;

}

|