登录 | 首页 -> 华新鲜事 -> 求学狮城 | 切换到:传统版 / sForum | 树形列表
JAVA算法问题,请cs高手帮忙
<<始页  [1]  末页>> 

JAVA算法问题,请cs高手帮忙最近在做cs3214 project, 编forum时有一个过滤器的问题不能解决, 请大侠们帮忙,万分感激。
requirement: E-moderator: Vulgar words or phrases are automatically replaced with hexes (‘###’). Strictly bans any occurrence of words/phrases that appear in the banned-words list, even if users deliberately join them together with other
words/punctuation to try and escape the ban.
就是说,不论是"shit", "s h i t" 还是"s^h#i*t", 都会被变成相对应的"####", "# # # #", or "#^###*#".
自己想来想去也找不到好算法,就靠各位高手们帮忙啦。如果能提供一些相关code最耗,不管怎样,小弟先谢过了。
[不行了 (10-23 16:05, Long long ago)] [ 传统版 | sForum ][登录后回复]1楼

hope this worksi think u can try the following approach, store the words into a vector,
(or a trie, if program efficienty is impt). and the just use the same method
the merge step from merge sort.

In fact, i doubt if your approach is really work. For exmaple:
if u meet two word shioth, and shiito, will u filter them or not? they are
'legal' words from dictionary.
[sc2 (10-23 21:39, Long long ago)] [ 传统版 | sForum ][登录后回复]2楼

hmm... try this algorithm假设要过滤的单词是 shit ..

那么, scan 整个 输入的String. 只提取字母, 其余的字符不管. (也就是说, 对于s^hi^t, 只会scan shit, 而忽略掉^)

scan 的同时会把 scan 的字符写入一个新的 String.

遇到第一个字母 s 或者 S 的时候, 创建一个 size 4 的 array. array[0]存储 s 的位置.

继续 scan , 如果下一个遇到 h, 就在array[1]存储 h 的位置. 如果不是 h, 就把array[0]清空. 如果是 s, 那么update array[0].

以此继续, 直到 array 被装满为止. 那么在写入 String 的时候, 就改成对应的写入 ####. 然后把array清空, 继续下一轮的scan.


如果要过滤多个单词, 可以创建多个array.

[辰星 (10-23 22:25, Long long ago)] [ 传统版 | sForum ][登录后回复]3楼


<<始页  [1]  末页>> 
登录 | 首页 -> 华新鲜事 -> 求学狮城 | [刷新本页] | 切换到:传统版 / sForum