7-23 Document Distance
Exp++
- 计算文件的嵌入向量的夹角,不考算法,是一个程序化题目
- 利用的sstream用于行处理,map和set用于统计相关变量,cmath.acos计算
- 脱离出函数的原因: 1)避免重复2-3遍; 2)模块化更清晰; 两个属性的对象用map就行,3个再用类,类数组采用逐个推入
- cin读取一定记得getline(cin,str);
- getline(cin,input); input>>ss; ss下次再输入前,要ss.clear()
//雷达初始化,
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<cmath>
#include<iomanip>
using namespace std;//title对应freq,
// class file{
// public:
// string title;
// map<string,int> freq; //all words can be read,but what's essential is just the frequency
// };double distance(map<string,int> dict1,map<string,int> dict2){//connstruct w1~wnset<string> word_list;for(auto i:dict1) //dict键值不重复word_list.insert(i.first);for(auto i:dict2){if(word_list.find(i.first)==word_list.end())word_list.insert(i.first);}vector<int> F1,F2;for(auto i:word_list){if(dict1.find(i)==dict1.end())F1.push_back(0);else F1.push_back(dict1[i]);if(dict2.find(i)==dict2.end())F2.push_back(0);else F2.push_back(dict2[i]);}//calculat4e angle valuedouble product=0,norm1=0,norm2=0;for(int i=0;i<word_list.size();i++){product+=F1[i]*F2[i];norm1+=F1[i]*F1[i];norm2+=F2[i]*F2[i];}norm1=sqrt(norm1);norm2=sqrt(norm2);return acos(product/(norm1*norm2));
}int main(){string str;int N; cin>>N; getline(cin,str);map<string,map<string,int>> file_list;for(int i=0;i<N;i++){// file tem_file;string title;cin>>title; getline(cin,str);string input; //read a linestring word; //read a wordmap<string,int> dict;getline(cin,input);stringstream ss;while(input!="#"){ss<<input;while(ss>>word){if(word=="#")break;else if(dict.find(word)==dict.end())dict.insert(make_pair(word,1));else dict[word]++;}ss.clear();getline(cin,input);}file_list.insert(make_pair(title,dict));}int M; cin>>M;for(int i=1;i<=M;i++){string t1,t2; cin>>t1>>t2;map<string,int> dict1,dict2;dict1=file_list[t1];dict2=file_list[t2];double result=distance(dict1,dict2);cout<<"Case "<<i<<": "<<fixed<<setprecision(3)<<result<<endl; //fixed小数点后输出}return 0;
}