Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
Register

## 255ms，贴代码，好想知道0ms的同学是怎么实现的

Posted by liduoldbiubiubiu at 2020-01-11 14:33:35 on Problem 1026
#include <iostream>
#include <string>
#include <vector>

int secret[201]={0};
char msg[200];
int loops[201]={0};
char en_msg[200];
using namespace std;
int main(){
while(true){
int n;
cin>>n;
if(n == 0){
break;
}

for(int i = 1; i<= n;i++){
cin>>secret[i];
}

for(int i=1;i<=n;i++){
if(loops[i] != 0){
continue;
}
int index = secret[i];
int loop = 1;
vector<int> pass_indices;
pass_indices.push_back(index);
while(index != i){
index = secret[index];
pass_indices.push_back(index);
loop++;
}
for(int j=0;j<pass_indices.size();j++){
int index = pass_indices[j];
loops[index]=loop;
}
}
int k;
cin>>k;
while(k != 0){
string s;
getline(cin,s);
s=s.substr(1);
int sl = s.length();
for(int i=0; i<n;i++){
if(i<sl){
msg[i]=s[i];
}else{
msg[i]=' ';
}
}
for(int i=0;i<n;i++){
int index = i+1;
int loop = loops[index];
int t = k%loop;
while(t>0){
index = secret[index];
t--;
}
en_msg[index-1] = msg[i];
}
for(int i=0;i<n;i++){
cout<<en_msg[i];
}
cout<<endl;

cin>>k;
}
for(int i=1;i<=n;i++){
loops[i]=0;
}
cout<<endl;
}
return 0;
}

Followed by: