CSP-2024-03-31第二题 相似度计算 个人的简单粗暴解法

lz是刷题萌新,这篇是写给自己用作记录的,大佬们如果不小心看到了可以帮我提提修改意见吗

一些非常耗时且费空间的100分解法↓难点居然是在这个输入如何处理换行符上!

#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
    int n, m;
    cin >> n >> m;
    string words1;
    string words2;
    cin.ignore(1, '\n');
    getline(cin, words1);
    getline(cin, words2);
    vector<string> dic1;
    vector<string> dic2;
    vector<string> newdiction1;
    vector<string> newdiction2;
    int index = 0;
    for (int i = 0; i < words1.length(); i++)
    {
        if (i == 0) { dic1.push_back(""); }
        if (words1[i] == ' ') { index++; dic1.push_back("");}
        else
        {
            if (words1[i] >= 'A' && words1[i] <= 'Z')
            {
                words1[i] = (char)(words1[i] - ('A' - 'a'));
            }
            dic1[index] += words1[i];
        }
    }
    index = 0;
    for (int i = 0; i < words2.length(); i++)
    {
        if (i == 0) { dic2.push_back(""); }
        if (words2[i] == ' ') { index++; dic2.push_back(""); }
        else
        {
            if (words2[i] >= 'A' && words2[i] <= 'Z')
            {
                words2[i] = (char)(words2[i] - ('A' - 'a'));
            }
            dic2[index] += words2[i];
        }
    }

    //去重
    for (int i = 0; i < dic1.size()-1; i++)
    {
        for (int j = i+1; j < dic1.size(); j++)
        {
            if (dic1[i] == dic1[j])
            {
                dic1[j] = "";
            }
        }
    }
    for (int i = 0; i < dic2.size() - 1; i++)
    {
        for (int j = i + 1; j < dic2.size(); j++)
        {
            if (dic2[i] == dic2[j])
            {
                dic2[j] = "";
            }
        }
    }
    //加入字典中
    for (int i = 0; i < dic1.size(); i++)
    {
        if (dic1[i] != "")
        {
            newdiction1.push_back(dic1[i]);
        }
    }
    for (int i = 0; i < dic2.size(); i++)
    {
        if (dic2[i] != "")
        {
            newdiction2.push_back(dic2[i]);
        }
    }
    //两个字典比较
    int _union = 0;
    int _conjunc = 0;
    for (int i = 0; i < newdiction1.size(); i++)
    {
        for (int j = 0; j < newdiction2.size(); j++)
        {
            if (newdiction1[i] == newdiction2[j]) { _union--; _conjunc++; }
        }
    }

    cout << _conjunc << endl;
    cout << newdiction1.size() + newdiction2.size() + _union ;

    return 0;
}

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值