回复 11# afan
最后试下:
#include <array.au3>
Global $out
Local $ts = TimerInit()
;~ Local $str[4] = ["abcde", "abcd", "abc","abs"]
;~ Local $str[3] = ["bc", "ca", "a"]
Local $str[6] = ["cabe", "bef", "abex","ca","be","efx"]
;~ Local $str[3] = ['gga', 'bcde', 'abc']
_ArraySort($str)
$fin = simplified($str)
;~ _ArrayDisplay($fin)
$t = _ArrayToString($fin, "")
MsgBox(0, TimerDiff($ts), $t)
Func simplified($array)
Local $flag_array[1], $flag
For $i = 0 To UBound($array) - 2
For $n = $i + 1 To UBound($array) - 1
$flag = StringRegExp($array[$i] & $array[$n], '(\w+)(?=[^\1]*\1)', 3)
If UBound($flag) > 0 Then
ReDim $flag_array[UBound($flag_array) + 1]
$flag_array[UBound($flag_array) - 1] = $flag[0]
EndIf
Next
Next
_ArrayDelete($flag_array, 0)
$new = _ArrayUnique($flag_array)
Local $long, $long_len = 0
For $i = 1 To UBound($new) - 1
If StringLen($new[$i]) > $long_len Then
$long = $new[$i]
$long_len = StringLen($long)
EndIf
Next
;~ _ArraySort($new)
;~ _ArrayDisplay($array)
;~ _ArrayDisplay($array)
;~ _ArrayDisplay($flag_array)
For $n = 0 To UBound($array) - 2
For $k = $n + 1 To UBound($array) - 1
Local $right = "", $left = ""
If $k > UBound($array) - 1 Then ContinueLoop
;~
If StringInStr($array[$n], $array[$k]) Then ;Or StringInStr($array[$k],$array[$n]) Then
_ArrayDelete($array, $k)
Return simplified($array)
ElseIf StringInStr($array[$k], $array[$n]) Then
_ArrayDelete($array, $n)
Return simplified($array)
Else
;~ _ArrayDisplay($array,$long)
If (StringRegExp($array[$n], '\b' & $long & '\w+\b', 0) Or StringRegExp($array[$n], '\b\w+' & $long & '\b', 0)) And _
(StringRegExp($array[$k], '\b' & $long & '\w+\b', 0) Or StringRegExp($array[$k], '\b\w+' & $long & '\b', 0)) Then
;~ $temp = StringRegExp($array[$n] & @CRLF & $array[$k], '\b' & $long & '\w+\b', 1)
;~ If Not @error Then $left = $temp[0]
;~
;~ $temp = StringRegExp($array[$n] & @CRLF & $array[$k], '\b\w+' & $long & '\b', 1)
;~ If Not @error Then $right = $temp[0]
;~
;~ $array[$n] = $right & StringReplace($left, $long, "")
$temp0 = StringRegExpReplace($array[$n] &";"& $array[$k], '(\w+)(?=;\1)', '')
$temp1 = StringRegExpReplace($array[$k] &";"& $array[$n], '(\w+)(?=;\1)', '')
If StringLen($temp0)<StringLen($temp1) Then
$array[$n] =StringReplace($temp0,";","")
_ArrayDelete($array, $k)
;~ _ArrayDisplay($array)
Return simplified($array)
Else
$array[$n] =StringReplace($temp1,";","")
_ArrayDelete($array, $k)
;~ _ArrayDisplay($array)
Return simplified($array)
EndIf
EndIf
EndIf
Next
Next
Return $array
EndFunc ;==>simplified
|