1004 Anagrams by Stack, 1004 anagrams
Evaluate the knowledge of the DFS application and use the stack to describe the string change process.
1 #include <stdio.h> 2 #include <string.h> 3 int len1,len2; 4 char str1[100],str2[100],stk[100],ans[200]; 5 6 void output(int n){ 7 int i; 8 for(i=0;i<n;i++){ 9 printf("%c ",ans[i]);10 }11 printf("\n");12 }13 14 void go(int in,int out,int p,int top){15 if(top<0)16 return;17 if(in==len1 && out==len2){18 output(p);19 return;20 }21 if(in<len1){22 stk[top]=str1[in];23 ans[p]='i';24 go(in+1,out,p+1,top+1);25 }26 if(top>0 && out<len2 && stk[top-1]==str2[out]){27 ans[p]='o';28 go(in,out+1,p+1,top-1);29 stk[top-1]=str2[out];30 }31 }32 33 int main(){34 while(scanf("%s%s",str1,str2)>=0){35 len1=strlen(str1);36 len2=strlen(str2);37 printf("[\n");38 if(len1==len2)39 go(0,0,0,0);40 printf("]\n");41 }42 return 0;43 }
ACM 1004 Anagrams by Stack
# Include "stdio. h"
# Include "string. h"
Char sequence [200];
Int lens, sp = 99, rsp = 0;
Char source [100], target [100], result [100], stack [100];
// Sequence [] record results
// Lens Word Length
// Sp stack top
// The location where the rsp outputs the result []
// Result [] result after io operation, which is compared with target []
Void clean ()
{
Int I;
For (I = 0; I <200; I ++)
{
Sequence [I] = '\ 0 ';
}
For (I = 0; I <100; I ++)
{
Source [I] = target [I] = '\ 0 ';
}
}
Void cleanstack ()
{
Int I = 0;
For (I = 0; I <100; I ++)
{
Result [I] = stack [I] = '\ 0 ';
}
Sp = 99;
Rsp = 0;
}
Int judge () // judge whether the stack operation is legal
{
Int counti, counto;
Int I, j;
Counti = counto = 0;
For (j = 0; j <lens * 2 + 1; j ++)
{
For (I = counti = counto = 0; I <j; I ++)
{
If ('I' = sequence [I])
{
Counti ++;
}
Else
{
Counto ++;
}
}
If (counto> counti | counto> lens | counti> lens)
{
Return 0;
}
}
Return 1;
}
Void push (char ch)
{
Stack [sp] = ch;
Sp --;
}
Void pop ()
{
Sp ++;
Result [rsp] = stack [sp];
Rsp ++;
}
Int check () // check whether the result after the io operation is consistent with the target
{
Int I = 0, ssp = 0;
While (I <lens * 2)
{
If (sequence [I] = 'I ')
{
Push (source [ssp]);
Ssp ++;
}
Else
{
Pop ();
}
I ++;
}
For (I = 0; I <lens; I ++)
{
If (result [I]! = Target [I])
{
Cleanstack ();
Return 0;
}
}
Cleanstack ();
Return 1;
}
Void output (int step) ...... remaining full text>
Anagrams by Stack who can help me translate
According to the original article, it should be the programming of the English reverse output in the computer.