Wikioi 1099 String transformation
Title Description
Description
It is known that there are two strings a$, b$, and a set of rules for the Transformation of strings (up to 6 rules):
a1$-b1$
a2$-b2$
The meaning of the rule is: the substring a1$ in a$ can be transformed to b1$, a2$ can be transformed into b2$ ....
Example: a$= ' ABCD ' b$= ' xyz '
The transformation rules are:
' The ' abc ', ' Xu ', ' ud ', ' y ' y ', ' yz '
At this point, the a$ can go through a series of transformations into b$, the process of which is:
' xyz ', ' xy ', ' xud ', ' ABCD '
A total of three transformations were made, making the a$ transform into b$.
Enter a description
Input Description
The input format is as follows:
a$ b$
a1$ b1$ \
a2$ b2$ |-> Transformation Rules
... ... /
The upper limit for all string lengths is 20.
Output description
Output Description
If the a$ is converted to b$ in 10 steps (including 10 steps), the minimum number of conversion steps is output, otherwise the output "NO answer!"
Sample input
Sample Input
ABCD XYZ
ABC Xu
UD y
Y yz
Sample output
Sample Output
3
Data range and Tips
Data Size & Hint
Hehe
Idea: Two-way wide search code:
1#include <stdio.h>2#include <string.h>3 structnode4 {5 Chars[ -];6 intDep//Number of Transformations7} list1[5010], list2[5010];8 Chara[7][ -], b[7][ -];9 intN;Ten voidBFS () One { A intHead1, Tail1, Head2, Tail2, K; -Head1 = Tail1 = head2 = Tail2 =1; - while(Head1 <= tail1 && head2 <=tail2) the { - if(LIST1[HEAD1].DEP + LIST2[HEAD2].DEP >Ten) - { -printf"NO answer!\n"); + return ; - } + for(inti =0; I < strlen (LIST1[HEAD1].S); i++) A for(intj =1; J <= N; J + +) at if(strncmp (List1[head1].s + i, A[j], strlen (a[j])) = =0)//find current rules that can be transformed - { -tail1++;//move the tail pointer, store the transformed string, the following three for loops for the transformation process - for(k =0; K < I; k++) -LIST1[TAIL1].S[K] =List1[head1].s[k]; - for(intL =0; L < strlen (B[j]); l++, k++) inLIST1[TAIL1].S[K] =B[j][l]; - for(intL = i + strlen (a[j]); L <= strlen (LIST1[HEAD1].S); l++, k++) toLIST1[TAIL1].S[K] =List1[head1].s[l]; +LIST1[TAIL1].S[K] =' /';//string plus terminator at end of transform -LIST1[TAIL1].DEP = list1[head1].dep+1; the for(k =1; K <= Tail1; k++) * if(strcmp (LIST1[TAIL1].S, list2[k].s) = =0)//determine if the current state is connected to a reverse search $ {Panax Notoginsengprintf"%d\n", LIST1[TAIL1].DEP +LIST2[K].DEP); - return ; the } + } A for(inti =0; I < strlen (LIST2[HEAD2].S); i++)//Reverse Search Ibid . the for(intj =1; J <= N; J + +) + if(strncmp (List2[head2].s + i, B[j], strlen (b[j])) = =0) - { $tail2++; $ for(k =0; K < I; k++) -LIST2[TAIL2].S[K] =List2[head2].s[k]; - for(intL =0; L < strlen (A[j]); l++, k++) theLIST2[TAIL2].S[K] =A[j][l]; - for(intL = i + strlen (b[j]); L <= strlen (LIST2[HEAD2].S); l++, k++)WuyiLIST2[TAIL2].S[K] =List2[head2].s[l]; theLIST2[TAIL2].S[K] =' /'; -LIST2[TAIL2].DEP = LIST2[HEAD2].DEP +1; Wu for(k =1; k <= Tail1; k++) - if(strcmp (LIST1[K].S, list2[tail2].s) = =0) About { $printf"%d\n", LIST1[K].DEP +LIST2[TAIL2].DEP); - return ; - } - } Ahead1++; +head2++; the } -printf"NO answer!\n"); $ } the intMain () the { thescanf"%s%s", list1[1].S, list2[1].s); then =1; - while(SCANF ("%s%s", a[n],b[n])! =EOF) inn++; then--; thelist1[1].DEP = list2[1].DEP =0; About BFS (); the return 0; the}View Code
Wide Search--transformation class