forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1004Anagrams by Stack.cpp
More file actions
79 lines (71 loc) · 1.83 KB
/
1004Anagrams by Stack.cpp
File metadata and controls
79 lines (71 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
//ZJU 1004 Anagrams by Stack
//2003.08.08 BY ADAI
#include <cstring>
#include <cmath>
using namespace std;
int main(void){
char in[50],out[50];
int len,i,j,top,n1,n2,t,size;
while(cin>>in>>out){
cout<<"["<<endl;
if(strlen(in)!=strlen(out)){
cout<<"]"<<endl;
continue;
}
len=strlen(in);
size=(pow(4.0,len)-3*pow(2.0,len)+2)/3+1;
int* a=new int[len*2];
char* b=new char[len];
for(i=0;i<len;i++){
a[i]=1;
a[i+len]=0;
}
for(i=0;i<size;i++){
top=n1=n2=t=0;
for(j=0;j<len*2;j++){
if(a[j]==1){
if(n1==len){
t=1;
break;
}
b[top]=in[n1];
top++;
n1++;
}
else{
if(top==0){
t=1;
break;
}
if(b[top-1]==out[n2]){
top--;
n2++;
}
else{
t=1;
break;
}
}
}
if(t==0){
for(j=0;j<len*2;j++){
if(a[j]==1) cout<<"i"<<" ";
else cout<<"o"<<" ";
}
cout<<endl;
}
for(j=len*2-1;j>=0;j--){
if(a[j]==0) a[j]=1;
else{
a[j]=0;
break;
}
}
}
cout<<"]"<<endl;
delete []a;
delete []b;
}
return 0;
}