2017百度前端编程 敏感词汇 字符串替换
来源:互联网 发布:中世纪骑兵知乎 编辑:程序博客网 时间:2024/05/22 06:10
字符串替换问题,出现给出的敏感词使用*代替
形如:输入:4
revolution
greatewall
democracy
science
cross the greatewall,we can reach every corner
输出:cross the *********,we can reach every corner2
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNext()){
int n = in.nextInt();
in.nextLine();
String[] sTest = new String[n];
for(int i = 0;i < n;i++){
sTest[i] = in.nextLine();
}
String str = in.nextLine();
//将字符串按逗号和空格进行分割,并且统计每个字符串的个数
String[] sArr = str.split("\\s+,?|,?\\s+");
Map<String, Integer> maps = new HashMap<String, Integer>();
for(int i = 0;i < sArr.length;i++){
if(maps.isEmpty()){
maps.put(sArr[i], 1);
}else if(maps.containsKey(sArr[i])){
int val = maps.get(sArr[i]);
maps.put(sArr[i], val+1);
}else{
maps.put(sArr[i], 1);
}
}
int l = 0,first = 0,end = 0;
for(int i = 0;i< n;i++){
if(maps.containsKey(sTest[i])){
int val = maps.get(sTest[i]);//字符串的个数,进行几次替换
while(val != 0){
l = sTest[i].length();//字符串的长度
first = str.indexOf(sTest[i]);//sTest在str出现的开始位置
end = first + l -1;//结束位置
str = replace(str, first, end);
val--;
}
}
}
System.out.println(str);
}
}
//字符串替换工作,将字符串从first到end之间的字符串替换为*
public static String replace(String string,int first,int end){
StringBuffer sb = new StringBuffer();
int l = end - first + 1;
for(int i = 0;i < first;i++){
sb.append(string.charAt(i));
}
for(int i = 0;i<l;i++){
sb.append("*");
}
for(int i = end + 1;i < string.length();i++){
sb.append(string.charAt(i));
}
return sb.toString();
}
}
- 2017百度前端编程 敏感词汇 字符串替换
- Android前端判断敏感词汇
- php 替换敏感字符串
- php 替换敏感字符串
- struts2的拦截器,替换掉敏感词汇
- 循环替换字符串,可用户替换敏感词
- 过滤器-过滤敏感词汇
- 屏蔽敏感词汇&KMP
- 屏蔽敏感词汇&KMP
- PHP 敏感词汇过滤
- 过滤敏感词汇
- 过滤敏感词汇
- 过滤敏感词汇
- 过滤敏感词汇
- 过滤敏感词汇
- 过滤敏感词汇
- 过滤敏感词汇
- 2017西安交大ACM小学期 敏感词汇[AC自动机]
- Qt实现菜单栏,工具栏,状态栏
- java编程思想(四)总结
- Hibernate的事物机制
- Python collections模块实例讲解
- packet_forwarder Use with Raspberry Pi
- 2017百度前端编程 敏感词汇 字符串替换
- (四)2进程调度的相关概念
- LaTeX(使用TexStudio)创建参考文献的方法
- 使用Fragment实现底部菜单栏功能,并实现左右滑动切换
- js onclick方法 传递json对象报错
- 判断tcp 是否正常连接
- java 四大特征(3)
- (四)3抢占和上下文切换
- Fedora系统安装MySQL后死活无法启动的问题