Dzień dobry.
Napisałem krótki program do znajdywania anagramów w tekście angielskim. Jednak program zgłasza double free or corruption (out)
, gdy chcę wczytać słowo zaczynające się od "
. To powszechne w linuxie?
#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
#include <cctype>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <utility>
#include <regex>
using namespace std;
char lower(char ch)
{
return char (tolower(ch));
}
void openFile(ifstream & fin)
{
cout << "Enter file name: ";
string fName;
getline(cin, fName);
fin.open(fName);
if (!fin.is_open())
exit(-1);
}
void readWords(ifstream & fin, vector<string> & words)
{
string word;
fin >> word;
cout << "OK";
while (fin)
{
transform(word.begin(), word.end(), word.begin(), lower);
regex e ("[\\^\\$\\\\\\.\\*\\+\\?\\(\\)\\[\\]\\{\\}\\|,;:'\\\"]+");
word = regex_replace(word, e, "");
words.push_back(word);
fin >> word;
}
fin.close();
}
void countLetters(vector<pair<char, int>> & letters, string & str)
{
if (str.size() > 0)
letters.push_back(make_pair(str[0], 1));
for (int i = 1; i < str.length(); i++)
for (int j = 0; j < letters.size(); j++)
{
if (letters[j].first == str[i])
letters[j].second++;
else if (isalpha(str[i]))
{
letters.push_back(make_pair(str[i], 1));
break;
}
}
}
string makeCompareStr(vector<pair<char, int>> & letters)
{
static const string Let = "abcdefghijklmnopqrstuvwxyz";
string cmpStr = "00000000000000000000000000";
for (auto it = letters.begin(); it != letters.end(); it++)
{
size_t pos = Let.find((*it).first);
cmpStr[pos] += (*it).second;
}
return cmpStr;
}
int searchAnagrams(vector<pair<vector<string>, int>> & an, string & str)
{
vector<pair<char, int>> letters;
vector<pair<char, int>> anLetters;
countLetters(letters, str);
for (auto it = an.begin(); it != an.end(); it++)
for (auto it2 = (*it).first.begin(); it2 != (*it).first.end(); it2++)
{
anLetters = vector<pair<char, int>> ();
if ((*it2) == str)
return 1000000000 + (it - an.begin());
countLetters(anLetters, *it2);
if ((makeCompareStr(letters) == makeCompareStr(anLetters)))
return it - an.begin();
}
return -1;
}
bool checkForExist(pair<vector<string>, int> & wordList, string & str)
{
for (auto it = wordList.first.begin(); it != wordList.first.end(); it++)
if (str == *it)
return true;
return false;
}
int main()
{
ifstream fin;
openFile(fin);
vector<string> words;
readWords(fin, words);
vector<pair<vector<string>, int>> anagrams;
for (auto it = words.begin(); it != words.end(); it++)
{
int i;
if ((i = searchAnagrams(anagrams, *it)) == -1)
{
pair<vector<string>, int> p = make_pair(vector<string> (), 0);
p.first.push_back(*it);
p.second = 1;
anagrams.push_back(p);
}
else if (i > 999999999)
anagrams[i % 1000000000].second++;
else
{
if (checkForExist(anagrams[i], *it))
anagrams[i].second++;
else
{
anagrams[i].first.push_back(*it);
anagrams[i].second++;
}
}
}
for (auto it = anagrams.begin(); it != anagrams.end(); it++)
{
for (auto it2 = (*it).first.begin(); it2 != (*it).first.end(); it2++)
cout << *it2 << " ";
cout << (*it).second << endl;
}
return 0;
}
plik
“Hi Amy! Your mum sent me a text. You forgot your inhaler. Why don’t you turn your phone on?” Amy didn’t like technology. She never sent text messages and she hated Facebook too.
Proszę o wskazówki
Dzięki.
M.