Topcoder SRM 392 DIV1 250

Source: Internet
Author: User

Test instructions: Give two strings a,b,a and B all contain lowercase English letters, and both contain and contain only one character ? , you now want the characters in A, b ? Replace each other with other strings (can be different, can be the same, can be empty), make a=b, and require the final A, B string (a=b) the shortest. Or the output is not possible.

Solution: Let a string as ? Number of the previous string, if A's ? is different from the B corresponding position, it is not possible if B's ? The character after the number is different from a corresponding position, it is not possible. Otherwise there must be solutions, violence to find the best solution can. This problem can be a hash or EXTEND-KMP method to achieve the complexity O (N) level, but the implementation of a little trouble, O (n^2) writing simple and easy.

My Code

#include <algorithm>#include <iostream>#include <sstream>#include <cstdlib>#include <cstring>#include <cstdio>#include <string>#include <vector>#include <cmath>#include <set>#include <map>using namespace STD;typedef Long Longllclasstwostringmasks{ Public:stringShortestcommon (stringS1,stringS2) {intT1 =0, t2 =0; for(inti =0; I < s1.length (); i++)if(S1[i] = =' * ') T1 = i; for(inti =0; I < s2.length (); i++)if(S2[i] = =' * ') t2 = i;if(T1 > T2) swap (S1, S2);stringANS1 ="", ANS3 =""; for(inti =0; I < s1.length (); i++)if(S1[i] = =' * ') ans1 = S1.substr (0, i); for(inti =1; I < s2.length (); i++)if(s2[i-1] ==' * ') Ans3 = S2.substr (i, s2.length ()-i); while(s1[0] !=' * '&& s2[0] !=' * ')        {if(s1[0]! = s2[0])return "Impossible"; S1.erase (0,1), S2.erase (0,1); } while(S1.back ()! =' * '&& s2.back ()! =' * ')        {if(S1.back ()! = S2.back ())return "Impossible";        S1.pop_back (), S2.pop_back (); } s1.erase (0,1);stringAns ="."; for(inti =-1; I < (int) S2.length (); i++) {if(I >=0&& S2[i] = =' * ') Break;stringS3; S3.clear (); for(intj =0; J <= I;            J + +) S3 + = S2[j]; S3 + = S1;BOOLOK =1;intT3 =0, t2 =0; while(T3 < s3.length () && T2 < S2.length () && OK) {if(S2[t2] = =' * ') Break;Else if(S3[T3]! = s2[t2]) OK =0;Elset3++, t2++; }if(S2[T2]! =' * ') OK =0;if(ok && (ans = ="."||            Ans.length () > (ans1 + s3 + ans3). Length ()) {ans = ans1 + s3 + ans3; }        }returnAns }};

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Topcoder SRM 392 DIV1 250

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.