forked from cielavenir/procon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtyama_PKU1119-TJU1576.java
64 lines (51 loc) · 1.6 KB
/
tyama_PKU1119-TJU1576.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//PKU1119
//どうしても正規表現を使いたくて苦手なJavaで^^;
import java.io.*;
import java.util.*;
import java.util.regex.*;
class Main{
public static Scanner cin;
public static Pattern pattern;
public static Matcher matcher;
public static String g(String s, String s1, String s2){
pattern = Pattern.compile(s1);
matcher = pattern.matcher(s);
return matcher.replaceAll(s2);
}
public static String[] parse(String s){ //Java is ByRef, so this func is destructive... But it is OK.
//文字種修正
s=g(s,"[^0-9a-zA-Z ]","");
//先頭空白削除
s=g(s,"^\\s*","");
if(s.equals(""))return null;
pattern = Pattern.compile(" +");
return pattern.split(s.toLowerCase()); //case-insensitiveですかい。
}
public static void main(String[] args){
int i,j;
cin=new Scanner(System.in);
cin.useDelimiter("----------"); //複数行もOKとは。。。ま、この辺はPerlの\/みたいな技法でw
String[] ss=parse(cin.next());
int[] ii=new int[ss.length];
for(;cin.hasNext();){
Arrays.fill(ii,0);
String s=cin.next();
String[] terms=parse(s);
if(terms==null)continue;
//集計
for(i=0;i<ss.length;i++)
for(j=0;j<terms.length;j++)
if(ss[i].equals(terms[j]))ii[i]++;
//重複単語の点をまとめる
for(i=0;i<ss.length;i++)
for(j=i+1;j<ss.length;j++)
if(ss[i].equals(ss[j])){
ii[i]+=ii[j];
ii[j]=0;
}
float f=0;
for(i=0;i<ss.length;i++)f+=Math.sqrt(ii[i]);
System.out.printf("%.2f\n",f);
}
}
}