The question is roughly the same,
One of the "paypalishiring" glyphs of the string "A" is this:
If one line reads and writes, it is Pahnaplsiigyir.
So, if the input Pahnaplsiigyir and 3, is the string and the number of layers, the output n trajectory of the character paypalishiring.
The first thought is how to split the input string, such as the above is 3 layers, divided into 3 strings, these three strings must be continuous
First paragraph + second paragraph + third paragraph
The challenge now is how to judge the length of each paragraph.
Abstract the above diagram
This is the case, each circle represents a character, which is actually regular. As follows
So there's a relationship between the number of each layer and the cycle.
Base= (total number of characters/(number of layers + layers-2))
Number of first layer =base+x
Last Layer =base+x
other layers =basex2+x;
The x in this is a compensation for a green circle, such as the following 4 green circles,
base=20/8=2
The first layer of =2+1=3;
Second layer =2x2+1=5;
The third layer =2x2+1=5;
Fourth floor =2x2+1=5;
The last layer of =2+0=2;
If this is the case
In fact, the algorithm is the same as long as the compensation for the X-value correction can be, this time will not discuss this situation.
When this step is reached, the previous partition is divided into 3 parts, then the N-Trace is traversed,
The initial scenario is to give three string numbers 1, 2, 3. Then follow the 12321232123 traversal until the end
So the question that needs to be solved now is how to implement this loop traversal.
The idea here is that there is a register in the CPU that determines whether an address is added or minus one, so borrowing this thought, the following scenario
The implementation functions are as follows
stringConvertstringTextintNrow)//text is the input string nrow is the number of layers{stringR vector <string>Mintarry[251]={0};intLen=text.size ();intS,c; s=len/(2*nrow-2);//base Datac=len% (2*nrow-2);//For the number of green above for(intI=1; i<=nrow;++i) {if(i==1)//Calculate the number of the first layer{if(c>=1) arry[i]=s+1;//arry The number of layers, content of the current layer ElseArry[i]=s; }Else if(I==nrow)//Calculate the number of the last layer{if(C>=nrow) arry[i]=s+1;ElseArry[i]=s; }Else ///number of middle tier{if(c>=i&&) arry[i]=2(SI1;Elsearry[i]=2*s; }} m.resize (nrow+1);intLocintBase Base=0; for(intI=1; i<=nrow;++i)//Divide the string according to the number of each layer{base+=arry[i-1]; for(intj=0; j<arry[i];++j) {loc=j+base; M[i].push_back (Text[loc]);//m[i] Save each layer of string,} }intflag=0;//~0 addr++ 0 add-- intCnt=1;int* num=New int[nrow+1]();//The following is the loop traversal after the split is complete intx, y;intTol=text.size (); while(1) {tol--;if(tol<0) Break; x=cnt; Y=NUM[CNT]; R.push_back (M[x][y]); num[cnt]++;if(Cnt==nrow | | cnt==1) Flag=~flag;if(flag!=0) cnt++;Elsecnt--; }cout<<r<<endl;returnR;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Traversing a string graph by a specific trajectory