Tag: Move
Password Encryption Problems
Personal information: He is a junior majoring in software engineering at yanda University;
My blog: Google searches for "cqs_2012;
My hobbies: I love data structures and algorithms and hope to make my own contribution to algorithm work in the future;
Programming Language: C ++;
Programming Environment: Windows 7 Professional Edition x64;
Programming tools: vs2010;
Drawing tools: Office 2010 PowerPoint;
Hardware information: 7g-3 Notebook;
Zhenyan
If you have not completed the task, it is not a task problem, it is your own problem.
Question
Best Practice 2818
Ideas
1. Violence Law, calculated once, moved once
2 optimization: computing is performed once, tracking is performed once, re-computing is performed, tracking is performed, and finally moving is performed.
3 optimization: calculate the number of times to move, calculate the final result, and then move
AC code
#include <iostream>#include <string>#include <vector>using namespace std;bool my_2818(vector<string> * myvector);int main(){bool sum = true;vector<string> * myvector = new vector<string>;vector<string>::iterator it;while(sum == true)sum = my_2818( myvector );it = myvector->begin();while(it < myvector->end()){cout<<*it<<endl;it++;}system("pause");return 0;}bool my_2818(vector<string> * myvector){int n;cin>>n;if(n == 0){return false;}int * data = new int[n];int *length = new int[n];for(int i=0;i<n;i++){cin>>data[i];length[i] = 0;}for(int j,i=0;i<n;i++){j = data[i]-1;length[i]++;while(true){if(j == i)break;j = data[j]-1;length[i]++;}}int k;string a;char* b = new char[n];char c;while(true){cin>>k;if(k == 0)break;for(int i=0;i<n;i++){b[i] = ' ';}getline(cin,a,'\n');a=a.substr(1,a.length()-1);while(a.length()<n)a+=' ';for(int i=0;i<n;i++){int j = k % length[i];int m = i;while(j>0){m = data[m]-1;j--;}b[m] = a[i];}string result="";for(int j=0;j < n;j++)result+=b[j];myvector->push_back(result);}myvector->push_back("");return true;}