吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 622|回复: 22
收起左侧

[经验求助] c++求助

[复制链接]
仿佛_一念成佛 发表于 2023-3-14 05:17
100吾爱币
本帖最后由 仿佛_一念成佛 于 2023-3-14 16:51 编辑

1.2.3.4|1.1.1.1|1.1.1.1|1.2.3.4|1.1.1.1

在一个vector string容器中有这么几个字符串。要求找出第一个重复的内容这个该怎么写啊,在上述容器中第一个出现的重复内容是1.1.1.1.


但是如果我又添加了一个1.2.3.4 使其变成这样1.2.3.4|1.1.1.1|1.1.1.1|1.2.3.4|1.1.1.1|1.2.3.4
那么第一个重复的内容应该是1.2.3.4,因为1.2.3.4在第一位


怪我没说清楚
当vector中含有“1.2.3.4", "1.1.1.1", "1.1.1.1", "1.2.3.4", "1.1.1.1","1.2.3.4"
有两个重复元素,分别是1.2.3.4 和 1.1.1.1并且数量相同。
那么程序返回的数量必须是1.2.3.4因为它的下标是第一位。
如果vector是这样子“1.2.3.4", "1.1.1.1", "1.1.1.1", "1.2.3.4", "1.1.1.1","1.2.3.4","1.1.1.1"
返回的应该是1.1.1.1 因为其有四位
“1.2.3.4", "1.1.1.1", "1.1.1.1", "1.2.3.4", "1.1.1.1","1.2.3.4","1.1.1.1","1.2.3.4"
返回的是1.2.3.4
总结就是当找到多个元素相同的时候返回下标优先的

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| 仿佛_一念成佛 发表于 2023-3-14 19:01
自己解决了

[C++] 纯文本查看 复制代码
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>

int findHighestFreqElementIndex(const std::vector<std::string> &elements)
{
    std::unordered_map<std::string, int> freq;
    for (int i = 0; i < elements.size(); i++)
    {
        freq[elements[i]]++;
    }

    int max_freq = 0;
    int max_index = -1;
    for (int i = 0; i < elements.size(); i++)
    {
        if (freq[elements[i]] > max_freq || (freq[elements[i]] == max_freq && max_index > i))
        {
            max_freq = freq[elements[i]];
            max_index = i;
        }
    }

    return max_index;
}

int main()
{
    std::vector<std::string> elements = {"1.2.3.4", "1.1.1.1", "1.1.1.1", "1.2.3.4", "1.1.1.1", "1.2.3.4", "1.1.1.1","1.2.3.4","8.8.8.8","8.8.8.8","8.8.8.8","8.8.8.8","8.8.8.8","1.2.3.4","6.6.6.6","6.6.6.6","6.6.6.6","6.6.6.6","6.6.6.6","6.6.6.6","6.6.6.6"};
    int result = findHighestFreqElementIndex(elements);
    std::cout << "Index of highest frequency element is: " << result << std::endl;
    std::cout << "Element is: " << elements[result] << std::endl;
    return 0;
}
鹿坪 发表于 2023-3-14 08:02
虽然1.2.3.4排在第一位,但它在第4位才重复,所以并没有什么毛病
lix0818 发表于 2023-3-14 08:35
如果你想要找到第一个元素的位置,可以在函数内部记录每个字符串出现的位置。在找到第一个重复的元素时,返回其第一次出现的位置即可。
lzj2299 发表于 2023-3-14 08:59
可以使用哈希表(unordered_map)来记录每个字符串出现的次数,如果发现某个字符串的出现次数大于1,就找到了第一个重复的内容。

下面是一个示例代码:

c++
Copy code
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;

int main() {
    vector<string> strs = {"1.2.3.4", "1.1.1.1", "1.1.1.1", "1.2.3.4", "1.1.1.1"};
    unordered_map<string, int> cnt;
    for (int i = 0; i < strs.size(); i++) {
        cnt[strs[i]]++;
        if (cnt[strs[i]] > 1) {
            cout << "第一个重复的内容是:" << strs[i] << endl;
            break;
        }
    }
    return 0;
}
输出结果为:

Copy code
第一个重复的内容是:1.2.3.4
注意,在哈希表中,字符串是作为键(key)存储的,而其出现次数是作为值(value)存储的。由于哈希表的查找速度非常快,因此这种算法的时间复杂度是O(n),其中n是字符串的数量。
WuYule 发表于 2023-3-14 09:18
为什么又加了1.2.3.4后第一个重复是1.2.3.4?我的理解是:1.1.1.1在下标2就重复了,1.2.3.4还要下标3才重复,那1.1.1.1才是第一个重复的吧。
Muujin 发表于 2023-3-14 09:19
[C++] 纯文本查看 复制代码
#include "bits/stdc++.h"
using namespace std;
string find_t(vector<string> &ans) {
    unordered_map<string, int> res;
    for(int i = 0; i < ans.size(); i++) {
        res[ans[i]]++;
        if(res[ans[i]] >= 2) {
            return ans[i];
        }
    }
    return "";
}

int main() {
    vector<string> ans1{"1234", "1111", "1234", "1111", "1234", "1111"};
    vector<string> ans2{"1234", "1111", "1111", "1234", "1111"};
    cout << find_t(ans1) << endl;
    cout << find_t(ans2) << endl;
    return 0;
}
Broadm 发表于 2023-3-14 09:37
[C++] 纯文本查看 复制代码
#include <iostream>
#include <thread>
#include <mutex>
#include <vector>
#include <unordered_map>
using namespace std;




int main() {

	vector<string> strs = { "1.2.3.4", "1.1.1.1", "1.1.1.1", "1.2.3.4", "1.1.1.1" };
	unordered_map<string, int> dic;

	//用来记录首次重复出现的位置
	int firstRepeatIndex = -1;

	for (int i = 0; i < strs.size(); i++)
	{
		auto& str = strs[i];
		unordered_map<string, int>::iterator iter;
		if ((iter = dic.find(str)) != dic.end())
		{
			cout << "重复的内容是:" << str << ", 首次出现的位置是: " << iter->second << ", 当前出现的位置是: " << i << endl;
			if (firstRepeatIndex == -1 || firstRepeatIndex > iter->second)
			{
				firstRepeatIndex = iter->second;
			}
		}
		else
		{
			dic[str] = i;
		}
	}

	if (firstRepeatIndex != -1)
	{
		cout << "第一个重复出现的内容是: " << strs[firstRepeatIndex] << endl;
	}

	return 0;
}


运行结果:
重复的内容是:1.1.1.1, 首次出现的位置是: 1, 当前出现的位置是: 2
重复的内容是:1.2.3.4, 首次出现的位置是: 0, 当前出现的位置是: 3
重复的内容是:1.1.1.1, 首次出现的位置是: 1, 当前出现的位置是: 4
第一个重复出现的内容是: 1.2.3.4
cjxxcgq 发表于 2023-3-14 09:40
不用unordered_map的解答,明显不如楼上效率高,哈哈

  vector<string> strs = { "1.2.3.4", "1.1.1.1", "1.1.1.1", "1.2.3.4", "1.1.1.1" };
    for (auto it = strs.begin(); it != strs.end(); it++)
    {
        bool bFind = false;
        for (auto it_in = it + 1; it_in != strs.end(); it_in++)
        {
            if (std::find(it_in, strs.end(), *it) != strs.end())
            {
                cout << "第一个重复的内容是:" << *it << endl;
                bFind = true;
                break;
            }
        }
        if (bFind) break;
    }
liu1024 发表于 2023-3-14 09:58
楼上说的对的
jmxjiamingxu 发表于 2023-3-14 10:19
第二个是1111吧?1111才是最先出现的重复 的字符串
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-24 04:35

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表