SJTU OJ 1133. Count the Stars Description
The director and the little partner were very bored at night, so they took his pet dog out for a walk. The director suddenly found that there was a string of N in the sky, and that the characters were all uppercase letters. So the director and his little friends began counting the Stars (star).
The director is not the same as his partner and his pet dog count the stars. A small partner is a very dogmatic person, he only likes the rules of things. So every time he would find the earliest ' s ' in the string, and then find ' s ' after the first ' t ', and then find ' t ' after the earliest ' a ', finally find ' a ' after the earliest ' R '. It is to find a sub-sequence of the star that is the most forward position. Each time a sub-sequence is found, small z counts to a star and then deletes the subsequence and continues until the star is found.
Pet dogs like simple things. Every time it just casually find s,t,a,r each, calculate a star, and the letter deleted.
The director did not like to delete the stars, so he randomly selected s,t,a,r each one, counted a star. But the director has a good memory, the star director of the same position combination will not repeat the number.
Now you have to count the director, his pet dog, and his little buddy to count up to a few stars, respectively.
Input Format
A total of 1 rows.
First line: A string s, no extra characters, and finally a newline character.
Output Format
Three integers, separated by a space, are the number of stars to which the director, the pet dog, and the small partner count.
Sample Input 1
STAR
Sample Output 1
1 1 1
Limits
30% n<=10 60% n<=60 100% n<=1000
After the analysis of the topic is actually to ask for three values (s = number of occurrences of s) .... )
¡1. s*t*a*r¡2. For min (s,t,a,r) ¡3. Ask for the maximum number of subsequence "STAR" The first two questions are very simple, mainly the last one to ask more trouble. Solution 1 is a purely analog approach to the simulation, the idea to the maximum number of sub-series will not exceed min (s,t,a,r) so you can traverse the coordinates of a sub-sequence s, and then the nearest t coordinates, the nearest a's coordinates, the nearest r coordinates
intstar[4][n];//0S 1T 2A 3 R The length of the second dimension indicates the number of characters, and the position of the characters is recorded .intcur[4]={0};//record the cursor used by the Fri//four characters currently go to the cursor if satisfies star[i][cur[i]]<star[j][cur[j]] for any of the i<j are established when the friend++; //Note: At most, only the Min star so the time of the cycle with min as the upper limit//The most important thing is to update the cursor so that it satisfies the criteria in order to set up a function to update//The Simulation method select the original S and then find T A R in turn for(inti =0; I < maximum; ++i) {cur[0]=i; if(Updatecur (1) && Updatecur (2) && Updatecur (3)){ ++Fri; }Else{ Break///current s position cannot find the star sub-sequence the next position must not be found}}//update flag cursor to be able to make Star[flag-1][cur[flag-1]]<star[flag][cur[flag]]BOOLUpdatecur (intflag) { //Max here is the min value of all Len, and Max means that it can use up to how many//If the condition is met, it can not be updated at all but returns the update successfully for(; Cur[flag] < maximum; + +Cur[flag])if(star[flag-1][cur[flag-1]]<Star[flag][cur[flag]])return true;//A successful update also means that the letter was deleted (? Will it cause the S position not to be updated successfully but deleted one more? ) return false;}
This method seems right ... But only 20 points ... I haven't found the wrong place yet ... Stay for later analysis ...
The second method: The scanning maintenance method is also an on-line calculation method (online calculation means: Stop the input at any time to get the results of the previous input)
The core of this idea is this, if a certain input after the s<t, then at least one T is not used, this is not used to refer to the fundamental even St is not formed because the s refers to the front of all the input S number,
So if s>t indicates that the current input T must be composed of St substrings, so the number of T refers to the amount of the St can be composed in sequence and so on
If we keep s>=t>=a, then the number of a refers to the amount of the STA substring that must be formed.
If you keep s>=t>=a>=r, then the last R represents the number of star that must be formed.
intS0), T (0), A (0), R (0); for(inti =0; I < n; ++i) {Switch(Sky[i]) { Case 'S': S++; Break; Case 'T': //Note that the S T-a in all subsequent maintenance conditions is the result of this input, so if s>t indicates that the new input T must be used//The use of this here is relative to the previous S, and ultimately can not be a complete sub-sequence is not known if(S>= (t+1))//if this t is used, then it's important to note that it must be used .t++; Break;//if this t does not constitute St at all, it is not credited Case 'A': if(t>=a+1) A++;//because the STA can be formed Break; Case 'R': if(a>=r+1) R++;//can form star Break; }} Fri= R;//the last R is the answer.
"Algorithmic Learning Notes" 26. Problem solving report of scanning maintenance method Sjtu_oj 1133 Stars