bestcoder#37_1001 string, Dfsrikka with stringaccepts:395submissions:2281Time limit:2000/1000 MS (java/others)Memory limit:65536/65536 K (java/others)Problem Description
As we know, Rikka is poor at math. Yuta is worrying on this situation, so he gives Rikka some math tasks to practice. There is one of the them:
One day, Yuta got a string which contains n letters is Rikka lost it in accident. Now they want to recover the string. Yuta remembers that the string was only contains lowercase letters and it was not a palindrome string. Unfortunately he cannot remember some letters. Can you help him recover the string?
It is too difficult for Rikka. Can you help her?
Input
This problem have multi test cases (no more than The first line contains a number n(1≤n≤1000 ) . The next line contains a n-length string which only contains lowercase letters and '? ', Haven place which Yuta are not sure .
Output
For each test cases print a n-length string–the string of come up with. In the case where more than one string exists, print the lexicographically rst one. In the case where no such string exists, output "QwQ".
Sample Input
5a?bb?3aaa
Sample Output
Aabbaqwq
Test instructions: Fill in the question mark with a letter so that the string is not a palindrome and the dictionary order is minimal
Idea: greedy, considering the smallest dictionary order, first sweep over the question mark to fill in the "a", and then sweep again to determine whether palindrome, if not directly output, if it is a palindrome, considering the minimum dictionary order, just change the right-most question mark at the B at the end of the output, but if the right-most question mark at the midpoint A special sentence can be skipped.
#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<string>#include<vector>using namespacestd;Const intmaxn=1000100;Const intInf= (1<< in);intN;stringS;vector<int>v;BOOLIspalin (strings) { for(intI=0; I<s.length (); i++){ if(s[i]!=s[n-1-I.])return false; } return true;}intMain () { while(cin>>n>>s) {v.clear (); BOOLflag=0; for(intI=0; i<n;i++){ if(s[i]=='?') {S[i]='a'; V.push_back (i); } } if(!ispalin (s)) cout<<s<<Endl; Else{ for(intI= (int) V.size ()-1; i>=0; i--){ if(v[i]!=n/2|| n%2==0) {S[v[i]]='b'; Flag=1; Break; } } if(flag) cout<<s<<Endl; Elsecout<<"QwQ"<<Endl; } } return 0;}View Code
bestcoder#37_1001 string, greedy