hmm... try this algorithm
登录 | 论坛导航 -> 华新鲜事 -> 求学狮城 | 本帖共有 1 楼,分 1 页, 当前显示第 1 页 : 本帖树形列表 : 刷新 : 返回上一页
<<始页  [1]  末页>>
作者:辰星 (等级:12 - 登峰造极,发帖:6227) 发表:2003-10-23 22:25:35  楼主  关注此帖评分:
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最耗,不管怎样,小弟先谢过了。
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.

Many events have slipped by.
And you are here, now, always.

People are not memories that you can put into words. They live.

欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版所有回复从这里展开收起列表
论坛导航 -> 华新鲜事 -> 求学狮城 | 返回上一页 | 本主题共有 1 篇文章,分 1 页, 当前显示第 1 页 | 回到顶部
<<始页  [1]  末页>>

请登录后回复:帐号   密码