我将要编写一个函数,它将返回一个最短周期的字母组合,这个组合最终可以创建给定的单词。
例如,单词abkebabkebabkeb是由重复的abkeb单词创建的。我想知道如何高效地分析输入的单词,以获取创建输入单词的最短字符周期。
例如,单词abkebabkebabkeb是由重复的abkeb单词创建的。我想知道如何高效地分析输入的单词,以获取创建输入单词的最短字符周期。
我想出了一个简单的解决方案,即使在非常大的字符串下也能完美运行。
PHP 实现:
function get_srs($s){
$hash = md5( $s );
$i = 0; $p = '';
do {
$p .= $s[$i++];
preg_match_all( "/{$p}/", $s, $m );
} while ( ! hash_equals( $hash, md5( implode( '', $m[0] ) ) ) );
return $p;
}
适用于 bcbdbcbcbdbc 等情况。
function smallestRepeatingString(sequence){
var currentRepeat = '';
var currentRepeatPos = 0;
for(var i=0, ii=sequence.length; i<ii; i++){
if(currentRepeat[currentRepeatPos] !== sequence[i]){
currentRepeatPos = 0;
// Add next character available to the repeat and reset i so we don't miss any matches inbetween
currentRepeat = currentRepeat + sequence.slice(currentRepeat.length, currentRepeat.length+1);
i = currentRepeat.length-1;
}else{
currentRepeatPos++;
}
if(currentRepeatPos === currentRepeat.length){
currentRepeatPos = 0;
}
}
// If repeat wasn't reset then we didn't find a full repeat at the end.
if(currentRepeatPos !== 0){ return sequence; }
return currentRepeat;
}
i = currentRepeat.length-1;
将i
重置为较小的值。因此,对于一个包含10个字符的字符串'aaaaaaaaab',需要46次迭代。对于一个包含20个字符的字符串,需要191次迭代。 - Buge非常抱歉回答晚了,但我在面试中得到了这个问题,以下是我的答案(可能不是最优的,但它也适用于奇怪的测试用例)。
private void run(String[] args) throws IOException {
File file = new File(args[0]);
BufferedReader buffer = new BufferedReader(new FileReader(file));
String line;
while ((line = buffer.readLine()) != null) {
ArrayList<String> subs = new ArrayList<>();
String t = line.trim();
String out = null;
for (int i = 0; i < t.length(); i++) {
if (t.substring(0, t.length() - (i + 1)).equals(t.substring(i + 1, t.length()))) {
subs.add(t.substring(0, t.length() - (i + 1)));
}
}
subs.add(0, t);
for (int j = subs.size() - 2; j >= 0; j--) {
String match = subs.get(j);
int mLength = match.length();
if (j != 0 && mLength <= t.length() / 2) {
if (t.substring(mLength, mLength * 2).equals(match)) {
out = match;
break;
}
} else {
out = match;
}
}
System.out.println(out);
}
}
测试用例:
abcabcabcabc
bcbcbcbcbcbcbcbcbcbcbcbcbcbc
dddddddddddddddddddd
adcdefg
bcbdbcbcbdbc
hellohell
代码返回:
abc
bc
d
adcdefg
bcbdbc
hellohell