63 lines
1.5 KiB
Plaintext
63 lines
1.5 KiB
Plaintext
class RepString {
|
|
function : Main(args : String[]) ~ Nil {
|
|
strings := ["1001110011", "1110111011", "0010010010", "1111111111",
|
|
"0100101101", "0100100", "101", "11", "00", "1"];
|
|
each(i : strings) {
|
|
string := strings[i];
|
|
repstring := RepString(string);
|
|
if(repstring->Size() > 0) {
|
|
"\"{$string}\" = rep-string \"{$repstring}\""->PrintLine();
|
|
}
|
|
else {
|
|
"\"{$string}\" = not a rep-string"->PrintLine();
|
|
};
|
|
};
|
|
}
|
|
|
|
function : RepString(string : String) ~ String {
|
|
offset := string->Size() / 2;
|
|
|
|
while(offset > 0) {
|
|
left := string->SubString(offset);
|
|
right := string->SubString(left->Size(),left->Size());
|
|
if(left->Equals(right)) {
|
|
if(ValidateMatch(left, string)) {
|
|
return left;
|
|
}
|
|
else {
|
|
return "";
|
|
};
|
|
};
|
|
|
|
offset--;
|
|
};
|
|
|
|
return "";
|
|
}
|
|
|
|
function : ValidateMatch(left : String, string : String) ~ Bool {
|
|
parts := string->Size() / left->Size();
|
|
tail := string->Size() % left->Size() <> 0;
|
|
|
|
for(i := 1; i < parts; i+=1;) {
|
|
offset := i * left->Size();
|
|
right := string->SubString(offset, left->Size());
|
|
if(<>left->Equals(right)) {
|
|
return false;
|
|
};
|
|
};
|
|
|
|
if(tail) {
|
|
offset := parts * left->Size();
|
|
right := string->SubString(offset, string->Size() - offset);
|
|
each(i : right) {
|
|
if(left->Get(i) <> right->Get(i)) {
|
|
return false;
|
|
};
|
|
};
|
|
};
|
|
|
|
return true;
|
|
}
|
|
}
|