[字符串]Very easy

本文介绍了一个使用Java实现的ACM竞赛题目解决方案,该方案通过计算两个文档之间的相似度来判断它们是否足够相似。具体实现包括读取输入、过滤不重要的词汇,并采用余弦相似度的方法进行比较。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Input

The test start with n which means that there are n cases.

The first line of each case gives two number p,q:the threshold and the size of excepted-word set.

The next line will give q words that you should ignore.

The next two lines are two document.

The document contains only words and all letters are lowercase.

The words are separated by blank space.

Output

If the two given documents are similar,print ’Yes’,or print ‘No’.

Sample Input

20.8 3i am ai am a boyi am a boy0.8 3i am ai am a boyi am a girl

Sample Output

YesNo

HINT

Source


第一次用Java做acm。

输入输出使用Scanner,配合String的split。非常方便

package A;
import java.util.*;
//import java.math.*;

public class Main {

	public static void main(String[] arvs)
	{
		Scanner a = new Scanner(System.in);
		int T = a.nextInt();
		while ((T--) > 0)
		{
			if (!a.hasNextDouble()) break;
			double p0 = a.nextDouble();
			int n = a.nextInt();
			HashSet<String> filter = new HashSet<String>();
			HashMap<String, Integer> hash1 = new HashMap<String,Integer>();
			HashMap<String, Integer> hash2 = new HashMap<String,Integer>();
			for (int i=0;i<n;i++)
			{
				String f = a.next(); 
				filter.add(f);
			}
			String line,words[];
			while ((line = a.nextLine()).equals(""));
			words = line.split(" ");
			for (String w:words)
			{
				if (filter.contains(w))
					continue;
				if (hash1.containsKey(w))
					hash1.put(w, hash1.get(w)+1);
				else
					hash1.put(w, 1);
			}
			
			double fenmu1 = 0;
			for (int frequence:hash1.values())
			{
				fenmu1 += frequence*frequence;
			}
			fenmu1 = Math.sqrt(fenmu1);
			
			while ((line = a.nextLine()).equals(""));
			words = line.split(" ");
			
			int common = 0;
			for (String w:words)
			{
				if (filter.contains(w))
					continue;
				if (hash1.containsKey(w))
				{
					common += hash1.get(w);
				}
				
				if (hash2.containsKey(w))
					hash2.put(w, hash2.get(w)+1);
				else
					hash2.put(w, 1);
			}
				
			double fenmu2 = 0;
			for (int frequence:hash2.values())
			{
				fenmu2 += frequence*frequence;
			}
			fenmu2 = Math.sqrt(fenmu2);
			
			double p = (double)common/(fenmu1*fenmu2);
			if (p < p0)
				System.out.println("No");
			else
				System.out.println("Yes");
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值