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.
那么, 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.
And you are here, now, always.
People are not memories that you can put into words. They live.