samedi 2 septembre 2023

I really don't understand what's wrong, why it doesn't work ? (c++, sports programming)

Any tips on how to make it faster? What are some quick fixes in general? My solution is to go beyond the time limit. I've tried changing the solution algorithm, but haven't come up with anything sensible. I tried to reduce the rs variable in the for loop by a factor of 2 to speed up the program, but in this case the output is WA.

Time limit 1 second Memory usage limit 64 MiB

To encrypt words, you can perform many different operations with them. For example, the following operation is interesting: the first few letters of a given word are added to its end in reverse order, and then removed from the beginning of the word. Thus the word a1a2...akak+1...an turns into the word ak+1...anakak-1...a1 (the number k is chosen in the range from 0 to n).

For two given words it is required to determine whether the described operation can be used to convert the first word into the second one.

Input data The input file consists of two lines - the first one contains the source word and the second one contains the expected result. The lengths of the lines do not exceed 50000 characters.

Output data In the first line of the output file print "Yes" if the conversion is possible and "No" if it is not. If the answer is yes, in the second line print k - the length of the relocated part of the source word k (choose the minimal one from all such k).

Example Input data: wpwdwpw wdwpwpw Output data: Yes

Code: lang-cpp

#include <string>
#include <vector>
#include <iostream>
using namespace std;
int main() {
    string Str2 = "";
    int i = 0, p = 0;
    string Strok1, Strok2; // <50000
    cin >> Strok1 >> Strok2;
    if (Strok1 == Strok2)
    {
        cout << "Yes" << endl << 0;
        return 0;
    }
    int rs = Strok1.size();
    for (i = 1; i <= rs; i++)
    {
        Str2 = Strok1[i - 1] + Str2;
        if (Strok1[i - 1] != Strok2[rs - i])
        {
            cout << "No";
            return 0;
        }
        if (Strok1.substr(i, rs - i) + Str2 == Strok2)
        {
            cout << "Yes" << endl << i;
            return 0;
        }
    }
    cout << "No";
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire