简体   繁体   English

使用SET(C ++)检查两个给定的字符串是否为字谜时出现运行时错误

[英]Getting run-time error while USING SET(C++) to check if two given strings are anagrams

Link to the problem: https://www.hackerearth.com/practice/basic-programming/input-output/basics-of-input-output/practice-problems/algorithm/two-strings-4/ 链接到问题: https : //www.hackerearth.com/practice/basic-programming/input-output/basics-of-input-output/practice-problems/algorithm/two-strings-4/

I have started with set and map these days. 这些天,我已经开始设置和映射。 Two strings str1 and str2 have equal length. 两个字符串str1和str2具有相等的长度。 I have to tell if they are anagrams of each other. 我必须告诉他们它们是否是彼此的字谜。 I used unordered_map to solve the problem by maintaining the count of characters which works in linear time and is great. 我使用unordered_map通过保持在线性时间内有效的字符数来解决此问题,这一点很棒。 But I want to use unordered_multiset but i am getting a run-time error. 但是我想使用unordered_multiset但我遇到了运行时错误。

code: 码:

#include<bits/stdc++.h>
using namespace std;
int main(){ 
int t;// number of testcases
cin>>t;
while(t--){
    string str1,str2;// two strings of equal length str1 and str2
    cin>>str1>>str2;
    unordered_multiset<char> s1,s2;// two sets 
    for(int i=0;i<str1.length();i++){
        s1.insert(str1[i]);// initialization
        s2.insert(str2[i]);
    }
    unordered_multiset<char>::iterator itr;
    for(itr=s1.begin();itr!=s1.end();itr++){
        if(s2.find(*itr)!=s2.end()) s2.erase(itr);/*  if *itr is present in s2 then delete its address .....
                                                    i know i am making mistake somewhere here but i can't figure out*/
        else {
            cout<<"NO"<<"\n";// print NO if not found
            break;
        }
    }
    if(itr==s1.end()) cout<<"YES"<<"\n";// if itr reached the end print YES
}
}

The idea is to loop through the set s1 and find the corresponding element in the set s2. 想法是遍历集合s1并在集合s2中找到对应的元素。 If not found print NO and break otherwise delete the corresponding element from s2 and since i am using iterator to delete elements so if there are multiple occurrences of a character then 1st occurrence should be deleted. 如果找不到,请打印NO并中断,否则从s2中删除相应的元素,并且由于我正在使用迭代器删除元素,因此,如果多次出现一个字符,则应删除第一次出现的字符。

please let me know if you didn't get my question 如果您没有收到我的问题,请告诉我

Try to solve it in a simpler way. 尝试以更简单的方式解决它。 The problem states that there are only lowercase characters, you can use only a frequency array for each word, an then compare it. 问题指出只有小写字符,每个单词只能使用一个频率数组,然后进行比较。

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        vector<int> f1(26, 0);
        vector<int> f2(26, 0);
        string s1, s2;
        cin >> s1 >> s2;
        for(const char& c : s1) f1[c - 'a']++;
        for(const char& c : s2) f2[c - 'a']++;
        cout << ( (f1 == f2)? "YES" : "NO") << endl;
    }
}

It's because you are using the iterator from s1 to erase an element from s2: 这是因为您正在使用s1中的迭代器来擦除s2中的元素:

if(s2.find(*itr)!=s2.end()) s2.erase(itr);

It have to be like this: 它必须是这样的:

if(s2.find(*itr)!=s2.end()) s2.erase(*itr);

or: 要么:

auto elem = s2.find(*itr);
if (elem != s2.end())
   s2.erase(elem);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM