- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring scramble.cpp
More file actions
Latest commit
133 lines (116 loc) · 3.22 KB
/
Copy pathstring scramble.cpp
File metadata and controls
133 lines (116 loc) · 3.22 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* http://leetcode.com/onlinejudge#question_87
http://blog.theliuy.com/scramble-string/ */
#include<iostream>
#include<math.h>
#include<time.h>
#include<vector>
usingnamespacestd;
boolisScrambled(string &s1, string &s2)
{
int len = s1.length();
if (len != s2.length()) returnfalse;
if (s1.compare(s2) == 0) returntrue;
unsignedint scode1 = 0;
unsignedint scode2 = 0;
for (int i = 0; i< len; ++i)
{
scode1 += s1[i];
scode2 += s2[i];
}
if (scode1 != scode2) returnfalse;
for(int i = 1; i < len; ++i)
{
if (isScrambled(s1.substr(0,i), s2.substr(0,i)) && isScrambled(s1.substr(i, s1.length() - i), s2.substr(i, s2.length()-i))
|| isScrambled(s1.substr(0,i), s2.substr(s2.length() - i,i))
&& isScrambled(s1.substr(i,s1.length()-i), s2.substr(0,s2.length()-i)))
returntrue;
}
returnfalse;
}
boolisScrambledEx(string &s1, string &s2)
{
if (s1.length() == 0 || s2.length() == 0)
returnfalse;
int i = 0;
int j = s1.length() - 1;
if (s1.compare(s2) == 0)
{
returntrue;
}
else
{
if (s1.length() > 1)
{
if (isScrambledEx(s1.substr(0, i + 1), s2.substr(j - i, j))
&& isScrambledEx(s1.substr(i + 1, j), s2.substr(0, j - i)))
returntrue;
if(isScrambledEx(s1.substr(0, j - i), s2.substr(i + 1, j))
&& isScrambledEx(s1.substr(j - i, j), s2.substr(0, i + 1)))
returntrue;
if (isScrambledEx(s1.substr(0, i + 1), s2.substr(0, i + 1))
&& isScrambledEx(s1.substr(i + 1, j), s2.substr(i + 1, j)))
returntrue;
if (isScrambledEx(s1.substr(0, j - i), s2.substr(0, j - i))
&& isScrambledEx(s1.substr(j - i, j), s2.substr(j - i, j)))
returntrue;
if (isScrambledEx(s1.substr(0, (i + j)/2 + 1), s2.substr(0, (i + j)/2 + 1))
&& isScrambledEx(s1.substr((i + j)/2 + 1, j), s2.substr((i + j)/2 + 1, j)))
returntrue;
if (isScrambledEx(s1.substr(0, (i + j)/2 ), s2.substr(0, (i + j)/2))
&& isScrambledEx(s1.substr((i + j)/2, j), s2.substr((i + j)/2, j)))
returntrue;
returnfalse;
}
else
returnfalse;
}
}
// dynamic programming.
// assume i -- the start index on s1, j -- the start index on s2, p -- the size of string.
// state function f(i, j, k) = true if for any n = 1...k-1,
// f(i, j + n, k - n) == true && f(i + k - n, j, n) == true
// or f(i, j, n) == true && f(i + n, j + n, k - n) == true;
boolisScrambleDP(string s1, string s2) {
int n = s1.size();
vector<vector<vector<int>>> m(n + 1, vector<vector<int>>(n + 1, vector<int>(n + 1, 0)));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
m[i][j][1] = (s1[i] == s2[j]);
}
}
for (int p = 2; p <= n; p++) {
for (int i = 0; i <= n - p; i++) {
for (int j = 0; j <= n - p; j++) {
for (int k = 1; k <= p - 1; k++) {
if (m[i][j][k] && m[i + k][j + k][p - k]) {
m[i][j][p] = 1;
break;
} elseif (m[i][j+k][p-k] && m[i + p - k][j][k]) {
m[i][j][p] = 1;
break;
}
}
}
}
}
return m[0][0][s1.size()];
}
intmain()
{
string s1 = "abcd";//"abbbcbaaccacaacc";
string s2 = "badc";//"acaaaccabcabcbcb";//"badc"; "dbac"
__time32_t start, end;
_time32(&start);
if (isScrambleDP(s1, s2))
{
cout<<"TRUE";
}
else
{
cout<<"FALSE";
}
_time32(&end);
cout<<endl<<_difftime32(end, start);
getchar();
return0;
}