将串中后续重复单词替换成下划线

例题描述和简单分析

字符串,如下所示:

Here here here we go!

 

So they're finally here, performing for you

If you know the words, you can join in too

Put your hands together, if you want to clap

As we take you through this monkey rap!

需要将串中后续重复单词替换成下划线,结果如下:

Here ____ ____ we go!

 

So they're finally ____, performing for you

If ___ know the words, ___ can join in too

Put your hands together, __ ___ want to clap

As __ take ___ through this monkey rap!

解法及简要说明

在集算器中编写脚本 p1.dfx,如下所示:


A

1

=str.words@w()

2

>A1.run(if(asc(left(lower(~),1))>=97   &&   asc(left(lower(~),1))<=122,if(words.pos(lower(~)),~=fill("_",len(~)),words|=lower(~))))

3

=A1.concat()

简要说明:

设两个脚本参数:

1、参数 str 是这个串

2、参数 words 是个空集

A1 @w表示拆出 str 中的所有字符,英文拆成单词

A2 遍历 A1 序表,若首字母的小写为 a-z,则说明该成员是个英文单词,再判断这个单词的小写是否包含在参数 words 中,若是,则将该单词变为长度相当的下划线,否则把该单词的小写追加至参数 words 中

A3  序表 A1 再拼成串

问答搜集

https://stackoverflow.com/questions/59309109/how-could-we-replace-repeated-words-by-underscores-java