Backwards is a good technique for dealing with strings and so on. For example, it has nothing to do with this problem, but the difficulty and meaning are better.
For example, a non-exact match is one character mismatch.
Well, there's actually a good way to do it:
Pattern string: AACB
A string to match:
1, Aamb
2. ACB
3, Aamdb
This time can actually match first, calculate the number of matching characters P1, then reverse matching, calculate the number of matches P2, and then see the relationship between P1+P2 and the length of the pattern string
Well, above ... Haven't done any specific questions yet.
Look at this problem again, but also the idea of handling strings backwards: O (n) Disposed of
#include <cstdio> #include <cstring> #include <iostream> #include <cstdio> #include < algorithm>using namespace Std;const int SIZE = 1000000+1;char Str[size];char ans[size*2];int leng;void Solve () { Leng = strlen (str); int blank = 0; for (int i=0;i<leng;i++) { if (str[i] = = ") blank++; } int ansleng = Blank*2+leng; int oldptr = leng-1; int newptr = ansleng-1; Ans[ansleng] = ' + '; while (oldptr>=0) { if (str[oldptr] = = ") { ans[newptr]= ' 0 '; ans[--newptr]= ' 2 '; ans[--newptr]= '% '; } else{ ans[newptr]=str[oldptr]; } cout << newptr << " << oldptr << Endl; --newptr; --oldptr; } printf ("%s\n", ans);} int main () { //freopen ("03.txt", "R", stdin); while (Cin.getline (str,size)) { //cout << str << endl; Solve (); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Backwards processing thinking-including string matching and nine-degree 1510 substitution for empty Gegian-offer03