1.1924: 那些年寮里的崽儿们
- 题目
1924: 那些年寮里的崽儿们 - 分析
用Java来做的话,其实这道题可以用一个LinkedHashMap来保存输入的字符串和它出现的次数,因为LinkedHashMap遍历时的顺序就是插入时的顺序,所以正好满足题意。 - 我的代码
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Map<String,Integer> map = new LinkedHashMap<String,Integer>();
int cas = 0;
while(in.hasNext()){
map.clear();
int n = in.nextInt();
in.nextLine();
for(int i=0; i<n; i++){
String str = in.nextLine();
Integer times = map.get(str);
if(times == null){
times = 0;
}
times++;
map.put(str, times);
}
System.out.printf("Case %d:\n",++cas);
Set<String> set = map.keySet();
for(String str: set){
System.out.printf("%s %d\n",str,map.get(str));
}
}
}
}
2.
- 题目
1925: Apache的情书 - 分析
可以使用java中String的replaceAll()函数来做,但我不会进行正则表达式的有效匹配,所以只好写了16种情况。希望有知道如何可以用一个匹配写出来,请在评论里面告诉我,谢谢! - 我的代码
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
StringBuffer sb = new StringBuffer();
String str;
while(in.hasNext()){
str =in.nextLine();
str = str.replaceAll("love", "hate");
str = str.replaceAll("lovE", "hatE");
str = str.replaceAll("loVe", "haTe");
str = str.replaceAll("loVE", "haTE");
str = str.replaceAll("lOve", "hAte");
str = str.replaceAll("lOvE", "hAtE");
str = str.replaceAll("lOVe", "hATe");
str = str.replaceAll("lOVE", "hATE");
str = str.replaceAll("Love", "Hate");
str = str.replaceAll("LovE", "HatE");
str = str.replaceAll("LoVe", "HaTe");
str = str.replaceAll("LoVE", "HaTE");
str = str.replaceAll("LOve", "HAte");
str = str.replaceAll("LOvE", "HAtE");
str = str.replaceAll("LOVe", "HATe");
str = str.replaceAll("LOVE", "HATE");
System.out.println(str);
}
}
}