I'm trying to solve the Scrambled Words problem in Google Kickstart 2018, Round A.
I'm having trouble generating the input string. Here are the directions they gave
The third line contains two lowercase English letters S1 and S2, and five integers N, A, B, C and D. S1 and S2 are the first two characters of the professor's string S, N is the length of S, and the other four integers are parameters that you should use to generate the characters of S, as follows:
First we define ord(c) as the decimal value of a character c and char(n) as the character value of a decimal n. For example, ord('a') = 97 and char(97) = 'a'. You can refer to ASCII table for other conversions.
Now, define x1 = ord(S1), x2 = ord(S2). Then, use the recurrence below to generate xi for i = 3 to N:
- xi = ( A * xi-1 + B * xi-2 + C ) modulo D.
We define Si = char(97 + ( xi modulo 26 )), for all i = 3 to N.
Using these directions for the test input,
1
5
axpaj apxaj dnrbt pjxdn abd
a a 50 1 1 1 30
The string that I am generating is
aapaapaapaapaapaapaapaapaapaapaapaapaapaapaapaapaa
But the string that is supposed to be generated is
aapxjdnrbtvldptfzbbdbbzxtndrvjblnzjfpvhdhhpxjdnrbt
Here is my code
char S1, S2;
long N, A, B, C, D;
cin >> S1 >> S2 >> N >> A >> B >> C >> D;
A %= D;
B %= D;
C %= D;
vector<char> S;
S.push_back(S1);
S.push_back(S2);
for (int i = 2; i < N; i++) {
long xi1 = (long)S[i - 1];
long xi2 = (long)S[i - 2];
long xi = (A * xi1) % D;
xi += ((B * xi2) % D);
xi += C;
xi %= D;
xi %= 26;
char Si = (char)(97 + xi);
S.push_back(Si);
}
Aucun commentaire:
Enregistrer un commentaire