PROBLEM LINK:
Author: rocknot
Tester: root_rvl
Editorialist: rocknot
DIFFICULTY:
EASY
PREREQUISITES:
string
PROBLEM:
Meena recently watched a detective movie and after watching the movie he decided to enroll in a detective course. But at the time of enrollment, he found out that in order to start this course he need to give a basic aptitude test. He solved almost all the questions in this test except one. It’s an encryption question and he doesn’t know anything about encryption so he asked you for help.
The question is as follows, You are given a string S and you need to find the Kth character of that string. But it is not as simple as it seems the string is encrypted so you need to find the original string before finding its Kth character. The encryption is as follows each repeated consecutive substring of the string is replaced by a single occurrence of that string followed by an number(1 to 9) denoting the no of repetition of that string.
Example “abcd3” denotes that substring “abcd” is repeated 3 times, so the original string will be “abcdabcdabcd”. You need to help Meena in order to solve this problem.
EXPLANATION:
simplify the string and return the Kth element
SOLUTIONS:
Setter's Solution
#include <bits/stdc++.h>
using namespace std;
int main(){
int T;
cin>>T;
while(T--){
int K;
string S,S1="",s3="";
cin>>K>>S;
int ctr=1;
for(int i=0;i<S.length();i++){
S1.push_back(S[i]);
if(S[i]>'0'&&S[i]<='9'){
S1.pop_back();
ctr--;
int ctr2=ctr;
int temp=(S[i]-'1')*S1.length(),pos=K-ctr;
ctr+=temp;
if(ctr>=K){
while(pos>S1.length()){
pos-=S1.length();
}
cout<<S1[pos-1]<<endl;
break;
}
S1="";
}
if(ctr==K){
cout<<S[i]<<endl;
break;
}
ctr++;
}
}
return 0;
}