3
3
#include < string.h>
4
4
#include < ctype.h>
5
5
6
+ bool IsMeetConstrainOfSchemaOfABABA (int lenOfStr, int lenOfA, int lenOfB)
7
+ {
8
+ return lenOfA % 2 == lenOfStr % 2 && (lenOfStr - 3 * lenOfA) % 2 == 0 && (lenOfB = (lenOfStr - 3 * lenOfA) / 2 ) >= 1 ;
9
+ }
10
+
11
+ bool IsStringEqual (char *str1, char *str2, int len)
12
+ {
13
+ int inc = 0 ;
14
+ while (inc < len && str1[inc] == str2[inc]) inc++;
15
+ return inc < len ? false : true ;
16
+ }
17
+
18
+ bool IsMeetSchemeOfABABA (char *str, int lenOfA, int lenOfB)
19
+ {
20
+ return IsStringEqual (str, str + lenOfA + lenOfB, lenOfA) &&
21
+ IsStringEqual (str, str + (lenOfA + lenOfB) * 2 , lenOfA) &&
22
+ IsStringEqual (str + lenOfA, str + 2 * lenOfA + lenOfB, lenOfB) &&
23
+ !(lenOfA == lenOfB &&IsStringEqual (str, str + lenOfA, lenOfA));
24
+ }
25
+
6
26
int IsSchemaOfABABA (char * str){
7
27
if (str == NULL || strlen (str) < 4 )
8
28
return false ;
9
29
int lenOfStr = strlen (str);
10
30
int lenOfA = 1 ;
11
31
int lenOfB = 1 ;
12
- int mod2OfStr = lenOfStr % 2 ;
13
32
for (lenOfA = 1 ; lenOfA < lenOfStr / 2 ; lenOfA++)
14
33
{
15
- // should meet the constrains about the relationship among lenOfA, lenOfB, lenOfStr
16
- if (lenOfA % 2 == mod2OfStr && (lenOfStr - 3 * lenOfA) % 2 == 0 && (lenOfB = (lenOfStr - 3 * lenOfA) / 2 ) >= 1 )
34
+ if (IsMeetConstrainOfSchemaOfABABA (lenOfStr, lenOfA, lenOfB = (lenOfStr - 3 * lenOfA) / 2 ))
17
35
{
18
- int lenOfAB = lenOfA + lenOfB;
19
-
20
- int inc = 0 ;
21
- while (inc < lenOfA && str[inc] == str[lenOfAB + inc]) inc++;
22
- if (inc < lenOfA) continue ;
23
-
24
- inc = 0 ;
25
- while (inc < lenOfA && str[inc] == str[lenOfAB * 2 + inc]) inc++;
26
- if (inc < lenOfA) continue ;
27
-
28
- inc = 0 ;
29
- while (inc < lenOfB && str[lenOfA + inc] == str[2 * lenOfA + lenOfB + inc]) inc++;
30
- if (inc < lenOfB) continue ;
31
-
32
- // A and B should not be same
33
- if (lenOfA == lenOfB){
34
- inc = 0 ;
35
- while (inc < lenOfA && str[inc] == str[inc + lenOfA])inc++;
36
- if (inc == lenOfA)
37
- continue ;
38
- }
39
-
40
- return true ;
36
+ if (IsMeetSchemeOfABABA (str, lenOfA, lenOfB))
37
+ return true ;
41
38
}
42
39
}
43
40
return false ;
44
41
}
45
42
43
+ bool IsMeetConstrainOfSchemaOfABABCAB (int lenOfStr, int lenOfAB, int lenOfC)
44
+ {
45
+ return lenOfC % 3 == lenOfStr % 3 && (lenOfStr - lenOfC) % 3 == 0 && (lenOfAB = (lenOfStr - lenOfC) / 3 ) >= 2 ;
46
+ }
47
+
48
+ bool IsSameAmongABC (char * str, int lenOfA, int lenOfB, int lenOfC)
49
+ {
50
+ return lenOfA == lenOfB && IsStringEqual (str, str + lenOfA, lenOfA) ||
51
+ lenOfA == lenOfC && IsStringEqual (str, str + (lenOfA + lenOfB) * 2 , lenOfA) ||
52
+ lenOfB == lenOfC && IsStringEqual (str + lenOfA, str + (lenOfA + lenOfB) * 2 , lenOfB);
53
+ }
54
+
55
+ bool IsMeetSchemeOfABABCAB (char *str, int lenOfAB, int lenOfC)
56
+ {
57
+ return IsStringEqual (str, str + lenOfAB, lenOfAB) && IsStringEqual (str, str + lenOfAB * 2 + lenOfC, lenOfAB);
58
+ }
59
+
46
60
int IsSchemaOfABABCAB (char * str){
47
61
if (str == NULL || strlen (str) < 6 )
48
62
return false ;
@@ -54,42 +68,15 @@ int IsSchemaOfABABCAB(char * str){
54
68
int lenOfB = 1 ;
55
69
for (lenOfC = 1 ; lenOfC <= lenOfStr - 6 ; lenOfC ++)
56
70
{
57
- // should meet the constrains of schema of ABABCAB
58
- if (lenOfC % 3 == mod3OfStr && (lenOfStr - lenOfC) % 3 == 0 && (lenOfAB = (lenOfStr - lenOfC) / 3 ) >= 2 )
71
+ if (IsMeetConstrainOfSchemaOfABABCAB (lenOfStr, lenOfAB = (lenOfStr - lenOfC) / 3 , lenOfC))
59
72
{
60
- int inc = 0 ;
61
- while (inc < lenOfAB && str[inc] == str[lenOfAB + inc]) inc++;
62
- if (inc < lenOfAB) continue ;
63
-
64
- inc = 0 ;
65
- while (inc < lenOfAB && str[inc] == str[lenOfAB * 2 + lenOfC + inc]) inc++;
66
- if (inc < lenOfAB) continue ;
67
-
73
+ if (!IsMeetSchemeOfABABCAB (str, lenOfAB, lenOfC)) continue ;
68
74
69
75
for (lenOfA = 1 ; lenOfA < lenOfAB;lenOfA++)
70
76
{
71
77
lenOfB = lenOfAB - lenOfA;
72
- if (lenOfA == lenOfB)
73
- {
74
- inc = 0 ;
75
- while (inc < lenOfA && str[inc] == str[lenOfA + inc]) inc++;
76
- if (inc == lenOfA) continue ;
77
- }
78
-
79
- if (lenOfA == lenOfC)
80
- {
81
- inc = 0 ;
82
- while (inc < lenOfA && str[inc] == str[lenOfAB * 2 + inc]) inc++;
83
- if (inc == lenOfA) continue ;
84
- }
85
-
86
- if (lenOfB == lenOfC)
87
- {
88
- inc = 0 ;
89
- while (inc < lenOfB && str[lenOfA + inc] == str[lenOfAB * 2 + inc]) inc++;
90
- if (inc == lenOfB) continue ;
91
- }
92
- return true ;
78
+ if (!IsSameAmongABC (str, lenOfA, lenOfB, lenOfC))
79
+ return true ;
93
80
}
94
81
}
95
82
}
0 commit comments