java字符串查找算法_java – 查找所有“字符相等”字符串的高效算法?

我们如何编写输出字符串“

homoglyph equivalents”的高效函数?

例1(伪代码):

homoglyphs_list = [

["o", "0"], // "o" and "0" are homoglyphs

["i", "l", "1"] // "i" and "l" and "1" are homoglyphs

]

input_string = "someinput"

output = [

"someinput", "s0meinput", "somelnput",

"s0melnput", "some1nput", "s0me1nput"

]

例2:

homoglyphs_list = [

["rn", "m", "nn"],

]

input_string = "rnn"

output = ["rnn", "rm", "mn", "rrn", "nnn", "nm", "nrn"]

例3:

homoglyphs_list = [

["d", "ci", "a"], // "o" and "0" are homoglyphs

["i", "l", "1"] // "i" and "l" and "1" are homoglyphs

]

/*

notice that with the two rules above,

we can infer "d" = "ci" = "a" = "cl" = "c1"

*/

input_string = "pacerier"

output = [

"pacerier", "pacerler", "pacer1er", "pcicerier",

"pcicerler", "pcicer1er", "pclcerier", "pc1cerier",

"pclcerler", "pc1cerler", "pclcer1er", "pc1cer1er",

"pdcerier", "pdcerler", "pdcer1er"

]

注意:输出数组中成员的顺序并不重要,我们可以假设给定的单纯形映射被认为是正确的(输入不会给我们一个“无限循环”).

我当前的算法有效,但它使用原始强制执行并且性能很糟糕.例如.带有同形字符[“rn”,“m”,“nn”]的“mmmmm”输入需要38秒才能运行:

// This is php code (non-pseudo just so we could test the running time),

// but the question remains language-agnostic

public function Func($in, Array $mappings){

$out_table = array();

$out_table[$in] = null;

while(true){

$number_of_entries_so_far = count($out_table);

foreach(array_keys($out_table) as $key){

foreach($mappings as $mapping){

foreach($mapping as $value){

for($a=0, $aa=strlen($key); $a

$pos = strpos($key, $value, $a);

if($pos === false){

continue;

}

foreach($mapping as $equal_value){

if($value === $equal_value){

continue;

}

$out_table[substr($key, 0, $pos) . $equal_value . substr($key, $pos + strlen($value))] = null;

}

}

}

}

}

if($number_of_entries_so_far === count($out_table)){

// it means we have tried bruteforcing but added no additional entries,

// so we can quit now

break;

}

}

return array_keys($out_table);

}

我们如何实现高效(快速)同形文字扩展算法?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值