A string with a passing rate of 1/3
Enter a string of S and an empty string of T. There are two operations on the S string. One is to extract the S string header and put it at the end of the T string, and the other is to extract the S string and put it at the end of the T string. The requirement is to minimize the Lexicographic Order of the T string.
From the perspective of the question, it is a very obvious greedy idea. This step is actually close to the answer, but it should be noted that when the S string's header and tail are the same, we certainly want to extract smaller characters at this time, therefore, we need to compare the next character. But if the pointers pointing to the beginning and the end are both in the forward direction, the two characters are still the same, what should we do? This situation... Yes, it is the return string. It is a string like "abcdcba. At this time, the pointer is constantly walking in. When the pointer is judged as a return string, the two ends can take the next one. Another scenario is the "abccba" and "abcaba" strings ". We need to constantly move the pointer to the center to determine which side of the string should be obtained first to ensure the minimum Lexicographic Order of the T string.
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <algorithm> 5 using namespace std; 6 const int maxn = 2000+10; 7 char s[maxn]; 8 9 void solve(int n)10 {11 int i,l,r,cnt;12 l = 0;13 r = n-1;14 for(cnt=1; l<=r; cnt++)15 {16 bool left = false;17 for(i=0; l+i<=r; i++)18 {19 if(s[l+i] < s[r-i])20 {21 left = true;22 break;23 }24 else if(s[l+i] > s[r-i])25 {26 left = false;27 break;28 }29 if((l+i == r-i) && s[l+i] == s[r-i])30 {31 left = true;32 break;33 }34 if((l+i+1 == r-i) && s[l+i] == s[r-i])35 {36 left = true;37 break;38 }39 }40 //if(top == n-1) left = true;41 if(left) putchar(s[l++]);42 else putchar(s[r--]);43 if(n % 80 != 0)44 if(cnt % 80 == 0)45 printf("\n");46 }47 }48 49 int main()50 {51 int n,i;52 while(scanf("%d%*c",&n) == 1)53 {54 char x;55 memset(s,0,sizeof(s));56 for(i=0; i<n; i++)57 {58 scanf("%c%*c",&x);59 s[i] = x;60 }61 s[i] = ‘\0‘;62 solve(n);63 }64 return 0;65 }View code