基于词频的文件相似度

一开始没有仔细看题,心想stringstream分割一下单词即可。但是处理不了Ddd@Fff 

单词间以任何非英文字母隔开-> 任何非字母都表明一个单词结束了

#include<iostream>
#include<string>
#include<sstream>
#include<set>
#include<cstdio>
#include<iomanip>
using namespace std;
const int N=110;
int n;
set<string>s[N];
void store()
{
    for(int i=1;i<=n;++i)
    {
        string str="";
        while(true)
        {
            char c=getchar();
            if(c=='#') break;
            else if(isalpha(c))
            {
                if(c>='A'&&c<='Z')
                   c=c+32;
                str+=c;
            }
            else                                //不是字符一个单词就结束了,要考虑是否要进入set了
            {
                if(str.size()>=3)
                {
                    if(str.size()>10)
                        str=str.substr(0,10);
                    s[i].insert(str);
                }
                str="";                         //不管进不进set,都必须清空,开始记录下一个单词s
            }

        }
    }
}

void output()
{
    for(int i=1;i<=n;++i)
    {
        set<string>::iterator it;
        for(it=s[i].begin();it!=s[i].end();++it)
        {
            cout<<*it<<endl;

        }
        cout<<endl;
    }
}
int main()
{
    cin>>n;
    scanf("\n");
    store();
    //output();

    int m;
    cin>>m;
    for(int i=1;i<=m;++i)
    {
        int a,b;
        int sa,sb;
        cin>>a>>b;
        sa=s[a].size();
        sb=s[b].size();
        set<string>::iterator it;
        int same=0;
            for(it=s[b].begin();it!=s[b].end();++it)
                if(s[a].count(*it)!=0)
                same++;

        //cout<<sa<<' '<<sb<<' '<<same<<endl;
        double ans=100.0*same/(sa+sb-same);
        cout<<fixed<<setprecision(1)<<ans<<'%'<<endl;
    }

    return 0;

}

https://blog.csdn.net/qq_41231926/article/details/84918684?tdsourcetag=s_pcqq_aiomsg

参考了该博主的代码,并改了一下下数据结构,更简洁一点点

猜你喜欢

转载自blog.csdn.net/iroy33/article/details/88370588