Yzoi Easy Round 2_ palindrome string

Source: Internet
Author: User

Original link: http://laphets1.gotoip3.com/?id=18

Description

Give a string consisting of lowercase letters, some of which are dyed black, with? Said. The original string is not known

A palindrome string that now allows you to find the smallest possible string of dictionary order. Like ' A ', ' aba ', ' ABBA ' such a symmetric string called back

Text string.

Each test point has 5 sets of small test points.

Input

5 rows, 5 strings.

Output

5 rows, 5 strings. If no solution, output "orz,i can not find it!"

This topic is mainly the use of a greedy thought is the general idea is to first put all the question mark first will use the smallest letter ' a ' instead of if say no, then the last question mark with B to replace so the obtained will certainly be the best

The next step is the classification discussion:

if the given string does not have a question mark then directly determine if the string is a palindrome if it is, we will not be able to turn it into a palindrome. Here the direct output Orz will be the opposite if it is not a palindrome directly output the current string can be

next to the case where there is only one question mark, we will first determine if there is a case where there is only one question mark and the question mark is just in the middle of the odd string if there is no need to tube the question mark (he has no effect on whether the string is a palindrome) Then you just need to do the Palindrome check () the same as the output so if there is one or more question marks before the middle question mark, we just need to think of the string before the middle question mark again as a new string to find out the last question mark in his string and put all these question marks in a Again check () if it's a palindrome, then we'll change the last question mark in this new string to B .

In addition, for situations that do not meet the above (that is, the string is not an odd string and then there is one or more question marks), we can simply sweep all the question marks into a and record the last question mark and then perform a check () if it is not a palindrome then of course it is the optimal solution of the output can of course If it is a palindrome then also the last mark of the new string into the B output can be at this time is the best solution ...

The code is as follows:

#include <iostream> #include <cstdio>using namespace std;const int Maxn=10000;char s[maxn];int n,cnt,last; BOOL Check () {for (int i=1;i<=n;i++) if (S[i]!=s[n+1-i]) return False;return true;} void print () {for (int i=1;i<=n;i++) printf ("%c", S[i]);p rintf ("\ n");} void work () {if (cnt==1) {if (check ()) printf ("Orz,i can not find it!\n"); else{s[last]= ' A ';p rint (); return;}} int t=0;for (int i=1;i<last;i++) if (s[i]== '? ') t=i;for (int i=1;i<=n;i++) if (s[i]== '? ') S[i]= ' a '; if (check ()) s[t]= ' B ';p rint ();} void Solve () {cnt=0;for (int i=1;i<=n;i++) {if (s[i]== '? ') {cnt++;last=i;}} if (cnt==0) {if (check ()) {printf ("Orz,i can not find it!\n"); return;} Else{print (); return;}} if ((n&1) && (last== (n+1) >>1)) {work (); return;} for (int i=1;i<=n;i++) if (s[i]== '? ') S[i]= ' a '; s[last]= ' B ';p rint ();} int main () {freopen ("string.in", "R", stdin), Freopen ("String.out", "w", stdout), for (int t=1;t<=5;t++) {scanf ("%s", s +1); N=strlen (s+1); solve ();}}


Yzoi Easy Round 2_ palindrome string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.