【发布时间】:2011-12-20 23:17:54
【问题描述】:
我有一个数组:
$myArray=array(
'hello my name is richard',
'hello my name is paul',
'hello my name is simon',
'hello it doesn\'t matter what my name is'
);
我需要找到最常重复的子字符串(最少 2 个单词),可能是数组格式,所以我的返回数组可能如下所示:
$return=array(
array('hello my', 3),
array('hello my name', 3),
array('hello my name is', 3),
array('my name', 4),
array('my name is', 4),
array('name is', 4),
);
所以我可以从这个数组数组中看到每个字符串在数组中的所有字符串中重复的频率。
只有这样的方法吗?..
function repeatedSubStrings($array){
foreach($array as $string){
$phrases=//Split each string into maximum number of sub strings
foreach($phrases as $phrase){
//Then count the $phrases that are in the strings
}
}
}
我尝试了类似于上述的解决方案,但速度太慢,每秒处理大约 1000 行,有人能做得更快吗?
【问题讨论】:
-
您只需要重复次数最多的子字符串吗?还是您需要每个可能的子字符串的计数?这是两个截然不同的问题。
-
@BenLee:我真的只需要最常重复的子字符串,但如果可能的话,我想知道下一个是哪个。
-
@BenLee:最好的解决方案是运行速度最快的解决方案
-
@RichardLivingston:这个问题目前无法回答。要判断您的解决方案是否更快,我必须知道您的解决方案是什么。每秒 1000 行是非常相对的,这取决于您的硬件。删除伪代码,改为显示实际代码。
-
我同意 netcoder。执行取决于数组中字符串的大小以及字符串的数量。如果不知道这些行中的内容,每秒 1000 行并不是很有用。