33#include < string.h>
44#include < ctype.h>
55
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+
626int IsSchemaOfABABA (char * str){
727 if (str == NULL || strlen (str) < 4 )
828 return false ;
929 int lenOfStr = strlen (str);
1030 int lenOfA = 1 ;
1131 int lenOfB = 1 ;
12- int mod2OfStr = lenOfStr % 2 ;
1332 for (lenOfA = 1 ; lenOfA < lenOfStr / 2 ; lenOfA++)
1433 {
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 ))
1735 {
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 ;
4138 }
4239 }
4340 return false ;
4441}
4542
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+
4660int IsSchemaOfABABCAB (char * str){
4761 if (str == NULL || strlen (str) < 6 )
4862 return false ;
@@ -54,42 +68,15 @@ int IsSchemaOfABABCAB(char * str){
5468 int lenOfB = 1 ;
5569 for (lenOfC = 1 ; lenOfC <= lenOfStr - 6 ; lenOfC ++)
5670 {
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))
5972 {
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 ;
6874
6975 for (lenOfA = 1 ; lenOfA < lenOfAB;lenOfA++)
7076 {
7177 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 ;
9380 }
9481 }
9582 }
0 commit comments