Soluție HackerRank pentru Mixing proteins. Include cerința formatată, exemple, explicația pașilor și cod sursă.
- Problemă: Mixing proteins
Cerinta completa
Some scientists are working on protein recombination, and during their research, they have found a remarkable fact: there are 4 proteins in the protein ring that mutate after every second according to a fixed pattern. For simplicity, proteins are called [Expresie matematică indisponibilă în copia arhivată] (you know, protein names can be very complicated). A protein mutates into another one depending on itself and the protein right after it. Scientists determined that the mutation table goes like this:
A B C D
_ _ _ _
A| A B C D
B| B A D C
C| C D A B
D| D C B A
Here rows denote the protein at current position, while columns denote the protein at the next position. And the corresponding value in the table denotes the new protein that will emerge. So for example, if protein i is A, and protein i + 1 is B, protein i will change to B. All mutations take place simultaneously. The protein ring is seen as a circular list, so last protein of the list mutates depending on the first protein.
Using this data, they have written a small simulation software to get mutations second by second. The problem is that the protein rings can be very long (up to 1 million proteins in a single ring) and they want to know the state of the ring after upto [Expresie matematică indisponibilă în copia arhivată] seconds. Thus their software takes too long to report the results. They ask you for your help.
Input Format
Input contains 2 lines.
First line has 2 integers [Expresie matematică indisponibilă în copia arhivată] and [Expresie matematică indisponibilă în copia arhivată], [Expresie matematică indisponibilă în copia arhivată] being the length of the protein ring and [Expresie matematică indisponibilă în copia arhivată] the desired number of seconds.
Second line contains a string of length [Expresie matematică indisponibilă în copia arhivată] containing uppercase letters [Expresie matematică indisponibilă în copia arhivată],[Expresie matematică indisponibilă în copia arhivată], [Expresie matematică indisponibilă în copia arhivată] or [Expresie matematică indisponibilă în copia arhivată] only, describing the ring.
Constraints
[Expresie matematică indisponibilă în copia arhivată]
[Expresie matematică indisponibilă în copia arhivată]
Output Format
Output a single line with a string of length [Expresie matematică indisponibilă în copia arhivată], describing the state of the ring after [Expresie matematică indisponibilă în copia arhivată] seconds.
Sample Input 0
5 15
AAAAD
Sample Output 0
DDDDA
Explanation 0
The complete sequence of mutations is:
AAADD
AADAD
ADDDD
DAAAD
DAADA
DADDD
DDAAA
ADAAD
DDADD
ADDAA
DADAA
DDDAD
AADDA
ADADA
DDDDA
Limbajul de programare folosit: cpp14
Cod:
#include <cstdio>
#include <iostream>
#include <fstream>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <string>
#include <ctime>
#include <cassert>
#include <utility>
using namespace std;
#define MAXN 1000050
int N, K;
string S;
int A[MAXN];
int B[MAXN];
int main() {
cin.sync_with_stdio(false);
cin >> N >> K >> S;
for (int i = 0; i < N; i++) {
A[i] = S[i] - 'A';
}
for (int j = 30; j >= 0; j--) {
if (K & (1 << j)) {
for (int i = 0; i < N; i++) {
int o = (i - (1 << j)) % N;
if (o < 0) {
o += N;
}
B[o] = A[i] ^ A[o];
}
memcpy(A, B, sizeof(A));
}
}
for (int i = 0; i < N; i++) {
S[i] = A[i] + 'A';
}
cout << S << endl;
return 0;
}
Scor obtinut: 1.0
Submission ID: 464649205
Link challenge: https://www.hackerrank.com/challenges/pmix/problem
