7-10 集合相似度 (25 分) (模拟)

7-10 集合相似度 (25 分)
给定两个整数集合,它们的相似度定义为:Nc/N​t​​ ×100%。其中N​c
​​是两个集合都有的不相等整数的个数,N​t​​ 是两个集合一共有的不相等整数的个数。你的任务就是计算任意一对给定集合的相似度。

输入格式:
输入第一行给出一个正整数N(≤50),是集合的个数。随后N行,每行对应一个集合。每个集合首先给出一个正整数M(≤10 ^ 4 ​ ),是集合中元素的个数;然后跟M个[0,10^9]区间内的整数。

之后一行给出一个正整数K(≤2000),随后K行,每行对应一对需要计算相似度的集合的编号(集合从1到N编号)。数字间以空格分隔。

输出格式:
对每一对需要计算的集合,在一行中输出它们的相似度,为保留小数点后2位的百分比数字。

输入样例:
3
3 99 87 101
4 87 101 5 87
7 99 101 18 5 135 18 99
2
1 2
1 3
输出样例:
50.00%
33.33%

solution:
充分利用我上次讲过的STL,30行可AC

#include <bits/stdc++.h>
using namespace std;

int main()
{
	int t;
	cin >> t;
	vector<set<int> > st(t);
	for (int i = 0; i < t; ++i)
	{
		int n;
		cin >> n;
		for (int j = 0; j < n; ++j)
		{
			int temp;
			cin >> temp;
			st[i].insert(temp);
		}
	}
	int n;
	cin >> n;
	while (n--)
	{
		int a, b;
		cin >> a >> b;
		vector<int> x(10000), y(10000);
		vector<int>::iterator xit = set_intersection(st[a - 1].begin(), st[a - 1].end(), st[b - 1].begin(), st[b - 1].end(), x.begin());
		x.resize(xit - x.begin());
		vector<int>::iterator yit = set_union(st[a - 1].begin(), st[a - 1].end(), st[b - 1].begin(), st[b - 1].end(), y.begin());
		y.resize(yit - y.begin());
		cout << fixed << setprecision(2) << 1.00 * x.size() / y.size() * 100.0 << '%' << endl;
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Textrank算法是一种基于图论的文本关键词提取算法,其核心思想是通过计算文本中词语之间的相似度来构建一个图,然后通过图论算法计算每个词语的权重,最终提取出文本中的关键词。 具体实现步骤如下: 1. 定义一个函数,用于读取文本文件,并将文本按照句子进行切。可以使用Python内置的nltk库中的sent_tokenize函数来实现。 2. 定义一个函数,用于对文本中的每个句子进行词,并去除停用词。可以使用Python内置的nltk库中的word_tokenize函数和stopwords集合来实现。 3. 定义一个函数,用于计算两个词语之间的相似度。可以使用余弦相似度来计算,公式如下: $similarity(w_i, w_j) = \frac{\sum_{k=1}^{n}w_{i,k} \times w_{j,k}}{\sqrt{\sum_{k=1}^{n}(w_{i,k})^2} \times \sqrt{\sum_{k=1}^{n}(w_{j,k})^2}}$ 其中,$w_{i,k}$表示词语$w_i$在文本中第$k$个位置的权重,$n$表示文本中总共的词语数量。 4. 定义一个函数,用于构建词语之间的图。可以使用Python内置的networkx库来实现。 5. 定义一个函数,用于计算每个词语的权重。可以使用PageRank算法来计算,公式如下: $score(w_i) = (1-d) + d \times \sum_{w_j \in In(w_i)}\frac{similarity(w_j, w_i) \times score(w_j)}{\sum_{w_k \in Out(w_j)}similarity(w_j, w_k)}$ 其中,$d$是阻尼系数,一般取值为0.85,$In(w_i)$表示指向词语$w_i$的所有边的集合,$Out(w_j)$表示从词语$w_j$出发的所有边的集合。 6. 定义一个函数,用于提取文本中的关键词。可以根据每个词语的权重进行排序,选取权重最高的前几个词语作为关键词。 下面是完整的代码实现: ```python import nltk import networkx as nx from nltk.tokenize import sent_tokenize, word_tokenize from nltk.corpus import stopwords def read_file(file_path): with open(file_path, 'r') as f: text = f.read() sentences = sent_tokenize(text) return sentences def preprocess(sentences): stop_words = set(stopwords.words('english')) processed_sentences = [] for sentence in sentences: words = word_tokenize(sentence.lower()) words = [word for word in words if word.isalnum() and word not in stop_words] processed_sentences.append(words) return processed_sentences def similarity(word1, word2, sentences): word1_count = sum([1 for sentence in sentences if word1 in sentence]) word2_count = sum([1 for sentence in sentences if word2 in sentence]) word1_indices = [i for i, sentence in enumerate(sentences) if word1 in sentence] word2_indices = [i for i, sentence in enumerate(sentences) if word2 in sentence] common_indices = set(word1_indices).intersection(set(word2_indices)) numerator = len(common_indices) / (word1_count * word2_count) denominator = 1 / word1_count + 1 / word2_count return numerator / denominator def build_graph(words, sentences): graph = nx.Graph() graph.add_nodes_from(words) for i, word1 in enumerate(words): for j, word2 in enumerate(words): if i != j: weight = similarity(word1, word2, sentences) if weight > 0: graph.add_edge(word1, word2, weight=weight) return graph def textrank(sentences): words = set([word for sentence in sentences for word in sentence]) graph = build_graph(words, sentences) scores = nx.pagerank(graph, alpha=0.85) sorted_scores = sorted(scores.items(), key=lambda x: x[1], reverse=True) keywords = [word for word, score in sorted_scores[:10]] return keywords if __name__ == '__main__': file_path = 'sample.txt' sentences = read_file(file_path) processed_sentences = preprocess(sentences) keywords = textrank(processed_sentences) print(keywords) ``` 在运行代码之前,需要先下载nltk库的停用词集合,可以使用以下代码实现: ```python import nltk nltk.download('stopwords') ``` 运行代码后,会输出文本中的前10个关键词。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值