集合数组
先检查输入的单词是否分割正确,再进行统计。
注意点:
- 单词大小写不敏感
- 单词长度大于2且小于等于10,以非字母分割。
- 两个文件的单词总量如果有相同的单词,只统计一次。
#include <cstdio>
#include <algorithm>
#include <string>
#include <iostream>
#include <unordered_set>
#include <cctype>
using namespace std;
const int N = 105;
unordered_set<string> st[N];
void to_lower(string &str){
for(auto &c: str) c = tolower(c);
}
int main(){
int n;
string s, temp;
scanf("%d", &n);
getchar();
for(int i = 1; i <= n; i++){
while(1){
getline(cin, s);
if(s=="#") break;
to_lower(s);
for(int j = 0; j < s.size();){
int k = j;
while(k < s.size() && isalpha(s[k])) k++;
if(k-j >= 3) {
temp = s.substr(j, min(10, k-j));
st[i].insert(temp);
}
while(k < s.size() && !isalpha(s[k])) k++;
j = k;
}
}
}
int m, p, q;
scanf("%d", &m);
while(m--){
scanf("%d%d", &p, &q);
int tt = 0, ss = 0;
for(auto it: st[p]){
if(st[q].count(it)) ss++;
}
printf("%.1f%%\n", ss*100.0/(st[p].size()+st[q].size()-ss));
}
return 0;
}