Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <msclr\marshal_cppstd.h>
- #include <algorithm>
- #include <fstream>
- #include <string>
- #include <set>
- #include <windows.h>
- #include <chrono>
- #include <map>
- template<class T>
- struct equal : std::binary_function<T, T, bool> {
- bool operator () (const T& a, const T& b) { return a == b; }
- };
- namespace ANAGRAM {
- using namespace System;
- using namespace System::ComponentModel;
- using namespace System::Collections;
- using namespace System::Windows::Forms;
- using namespace System::Data;
- using namespace System::Drawing;
- /// <summary>
- /// Сводка для MyForm
- /// </summary>
- std::string FILEPATH = "";
- public ref class MyForm : public System::Windows::Forms::Form
- {
- public:
- MyForm(void)
- {
- InitializeComponent();
- //
- //дTODO: добавьте код конструктора
- //
- }
- protected:
- /// <summary>
- /// Освободить все используемые ресурсы.
- /// </summary>
- ~MyForm()
- {
- if (components)
- {
- delete components;
- }
- }
- private: System::Windows::Forms::Button^ button1;
- private: System::Windows::Forms::ListBox^ listBox1;
- private: System::Windows::Forms::TextBox^ textBox1;
- private: System::Windows::Forms::Button^ button2;
- private: System::Windows::Forms::Label^ label1;
- private: System::Windows::Forms::Button^ button3;
- private: System::Windows::Forms::Button^ button4;
- private: System::Windows::Forms::OpenFileDialog^ openFileDialog1;
- private: System::Windows::Forms::Button^ button5;
- private: System::Windows::Forms::Button^ button6;
- protected:
- private:
- /// <summary>
- /// Обязательная переменная конструктора.
- /// </summary>
- System::ComponentModel::Container ^components;
- #pragma region Windows Form Designer generated code
- /// <summary>
- /// Требуемый метод для поддержки конструктора — не изменяйте
- /// содержимое этого метода с помощью редактора кода.
- /// </summary>
- void InitializeComponent(void)
- {
- this->button1 = (gcnew System::Windows::Forms::Button());
- this->listBox1 = (gcnew System::Windows::Forms::ListBox());
- this->textBox1 = (gcnew System::Windows::Forms::TextBox());
- this->button2 = (gcnew System::Windows::Forms::Button());
- this->label1 = (gcnew System::Windows::Forms::Label());
- this->button3 = (gcnew System::Windows::Forms::Button());
- this->button4 = (gcnew System::Windows::Forms::Button());
- this->openFileDialog1 = (gcnew System::Windows::Forms::OpenFileDialog());
- this->button5 = (gcnew System::Windows::Forms::Button());
- this->button6 = (gcnew System::Windows::Forms::Button());
- this->SuspendLayout();
- //
- // button1
- //
- this->button1->Location = System::Drawing::Point(12, 43);
- this->button1->Name = L"button1";
- this->button1->Size = System::Drawing::Size(75, 23);
- this->button1->TabIndex = 0;
- this->button1->Text = L"1й вариант";
- this->button1->UseVisualStyleBackColor = true;
- this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click);
- //
- // listBox1
- //
- this->listBox1->FormattingEnabled = true;
- this->listBox1->Location = System::Drawing::Point(232, 17);
- this->listBox1->Name = L"listBox1";
- this->listBox1->Size = System::Drawing::Size(297, 238);
- this->listBox1->TabIndex = 1;
- //
- // textBox1
- //
- this->textBox1->Location = System::Drawing::Point(122, 17);
- this->textBox1->Name = L"textBox1";
- this->textBox1->Size = System::Drawing::Size(104, 20);
- this->textBox1->TabIndex = 2;
- //
- // button2
- //
- this->button2->Location = System::Drawing::Point(12, 72);
- this->button2->Name = L"button2";
- this->button2->Size = System::Drawing::Size(214, 23);
- this->button2->TabIndex = 3;
- this->button2->Text = L"Поиск всех групп анаграмм";
- this->button2->UseVisualStyleBackColor = true;
- this->button2->Click += gcnew System::EventHandler(this, &MyForm::button2_Click);
- //
- // label1
- //
- this->label1->AutoSize = true;
- this->label1->Location = System::Drawing::Point(13, 20);
- this->label1->Name = L"label1";
- this->label1->Size = System::Drawing::Size(103, 13);
- this->label1->TabIndex = 4;
- this->label1->Text = L"слово для поиска: ";
- //
- // button3
- //
- this->button3->Location = System::Drawing::Point(232, 261);
- this->button3->Name = L"button3";
- this->button3->Size = System::Drawing::Size(297, 23);
- this->button3->TabIndex = 5;
- this->button3->Text = L"clear listbox";
- this->button3->UseVisualStyleBackColor = true;
- this->button3->Click += gcnew System::EventHandler(this, &MyForm::button3_Click);
- //
- // button4
- //
- this->button4->Location = System::Drawing::Point(12, 260);
- this->button4->Name = L"button4";
- this->button4->Size = System::Drawing::Size(75, 23);
- this->button4->TabIndex = 6;
- this->button4->Text = L"select file";
- this->button4->UseVisualStyleBackColor = true;
- this->button4->Click += gcnew System::EventHandler(this, &MyForm::button4_Click);
- //
- // openFileDialog1
- //
- this->openFileDialog1->FileName = L"openFileDialog1";
- //
- // button5
- //
- this->button5->Location = System::Drawing::Point(12, 101);
- this->button5->Name = L"button5";
- this->button5->Size = System::Drawing::Size(214, 23);
- this->button5->TabIndex = 7;
- this->button5->Text = L"то же, что выше, но сильно быстрее";
- this->button5->UseVisualStyleBackColor = true;
- this->button5->Click += gcnew System::EventHandler(this, &MyForm::button5_Click);
- //
- // button6
- //
- this->button6->Location = System::Drawing::Point(93, 43);
- this->button6->Name = L"button6";
- this->button6->Size = System::Drawing::Size(133, 23);
- this->button6->TabIndex = 8;
- this->button6->Text = L"он же, но чуть быстрее";
- this->button6->UseVisualStyleBackColor = true;
- this->button6->Click += gcnew System::EventHandler(this, &MyForm::button6_Click);
- //
- // MyForm
- //
- this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
- this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
- this->ClientSize = System::Drawing::Size(547, 301);
- this->Controls->Add(this->button6);
- this->Controls->Add(this->button5);
- this->Controls->Add(this->button4);
- this->Controls->Add(this->button3);
- this->Controls->Add(this->label1);
- this->Controls->Add(this->button2);
- this->Controls->Add(this->textBox1);
- this->Controls->Add(this->listBox1);
- this->Controls->Add(this->button1);
- this->MaximizeBox = false;
- this->MaximumSize = System::Drawing::Size(563, 340);
- this->MinimumSize = System::Drawing::Size(563, 340);
- this->Name = L"MyForm";
- this->Text = L"анаграмматор 300";
- this->ResumeLayout(false);
- this->PerformLayout();
- }
- #pragma endregion
- void dh(std::string message) {
- listBox1->Items->Insert(listBox1->Items->Count, cs(message));
- }void dh(String^ message) {
- listBox1->Items->Insert(listBox1->Items->Count, message);
- }
- // converts System::String to std::string
- std::string s(String^ str) {
- return msclr::interop::marshal_as<std::string>(str);
- }std::string s(System::Decimal dec) {
- return std::to_string(decToInt(dec));
- }template<typename T>
- std::string s(const T& value) { // converts ints, floats and anything it can to std::string
- return std::to_string(value);
- }
- // converts std::string to System::String^
- System::String^ cs(std::string str) {
- return gcnew String(str.c_str());
- }
- int decToInt(const System::Decimal& dec) {
- return dec.ToInt32(dec); // работает, лол
- }
- bool is_anagram(const std::string& word1, const std::string& word2) {
- if (word1.size() == word2.size()) {
- std::multiset<char> wrd{ word1.begin(), word1.end() };
- std::multiset<char> buf{ word2.begin(), word2.end() };
- if (buf == wrd) { return true; };
- }
- else return false;
- }
- std::vector<std::string> find(std::string word) {
- /// <summary>
- /// simple straight-forward search algorithm working with
- /// any type of files and utilizing multisets
- /// </summary>
- /// <param name="word"></param>
- /// <returns>std::vector<std::string></returns>
- std::vector<std::string> found_anagrams;
- std::ifstream file(FILEPATH);
- std::string buffer;
- std::multiset<char> wrd{ word.begin(), word.end() };
- if (!file.is_open()) { dh("file not opened"); }
- else { // !file.eof() instead of just while(file)
- while (!file.eof()) {
- getline(file, buffer);
- if (buffer.size() == word.size()) {
- std::multiset<char> buf{ buffer.begin(), buffer.end() };
- if (buf == wrd) { if(std::find(found_anagrams.begin(), found_anagrams.end(), buffer) != std::end(found_anagrams)) found_anagrams.push_back(buffer); };
- }
- }
- }
- file.close();
- return found_anagrams;
- }
- std::vector<std::string> find_with_cheats(std::string word) {
- std::vector<std::string> found_anagrams;
- std::ifstream file(FILEPATH);
- std::string buffer;
- std::multiset<char> wrd{ word.begin(), word.end() };
- if (!file.is_open()) { dh("file not opened"); }
- else {
- while (!file.eof()) {
- getline(file, buffer);
- if (buffer.size() == word.size()) {
- std::multiset<char> buf{ buffer.begin(), buffer.end() };
- if (buf == wrd) { found_anagrams.push_back(buffer); }
- else if (found_anagrams.size() != 0 && buf != wrd) {
- file.close();
- break;
- }
- }
- }
- }
- return found_anagrams;
- }
- void setFilePath() {
- openFileDialog1->Filter = "txt files (*.txt)|*.txt";
- if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK) {
- FILEPATH = s(openFileDialog1->FileName);
- dh(FILEPATH);
- }
- else { dh("unable to add file path"); }
- }
- std::vector<std::vector<std::string>> find_groups(uint8_t minimum_group_size_limit) { // честный поиск
- std::vector<std::vector<std::string>> groups_vector;
- std::string buffer;
- dh("opening file at "+FILEPATH);
- std::ifstream file(FILEPATH);
- std::vector<std::multiset<char>> multisets_vector;
- if (!file.is_open()) { dh("unable to access file"); }
- else {
- while (!file.eof()) {
- getline(file, buffer);
- std::multiset<char> buf{ buffer.begin(), buffer.end() };
- if(std::find(multisets_vector.begin(), multisets_vector.end(), buf) == multisets_vector.end()){
- auto found = find(buffer);
- if (found.size() > minimum_group_size_limit) {
- groups_vector.push_back(found);
- multisets_vector.push_back(buf);
- }
- }
- }
- }
- return groups_vector;
- }
- void find_groups_cheating() { // читерский поиск
- std::string buffer;
- dh("opening file at " + FILEPATH);
- std::multiset<char> prev_set;
- std::ifstream file(FILEPATH);
- int c = 0;
- do {
- getline(file, buffer);
- std::multiset<char> buf{ buffer.begin(), buffer.end() };
- if(buf != prev_set){
- dh("----------next group: " + s(c++));
- prev_set = buf;
- dh(buffer);
- }
- else{ dh(buffer); }
- } while (!file.eof());
- file.close();
- dh("готово");
- }
- void find_all(std::string word) {
- dh("все возможные анаграммы cлова "+word+":");
- std::sort(word.begin(), word.end());
- do {
- dh(word);
- } while (std::next_permutation(word.begin(), word.end()));
- }
- private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
- std::string word = s(textBox1->Text->ToUpper());
- if (FILEPATH == "") setFilePath();
- else if (word == "") dh("введи хоть что-нибудь");
- else {
- auto begin = std::chrono::steady_clock::now();
- auto result = find(word); // ПОИСК
- auto end = std::chrono::steady_clock::now();
- auto elapsed_ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin);
- dh("поиск занял "+s(elapsed_ms.count())+" ms");
- if (result.size() != 1) {
- dh("найденные анаграммы для " + word + ":");
- for (auto el : result) if(el != word) dh(el);
- }
- else dh("анаграмм для "+word+" не найдено");
- }
- }
- private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
- if (FILEPATH == "") setFilePath();
- System::Windows::Forms::MessageBox ^box;
- if (box->Show("Чумба, ты действительно хочешь использовать этот метод? это займет вечность", "ты серьезно?", System::Windows::Forms::MessageBoxButtons::YesNo, MessageBoxIcon::Question) == System::Windows::Forms::DialogResult::Yes) {
- auto begin = std::chrono::steady_clock::now();
- auto groups = find_groups(1); // ПОИСК
- auto end = std::chrono::steady_clock::now();
- auto elapsed_ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin);
- dh("поиск занял " + s(elapsed_ms.count()) + " ms");
- dh("найдено групп:" + s(groups.size()-1));
- for (auto el : groups) {
- dh("----------next group:");
- for (auto str : el) {
- dh(str);
- }
- }
- }
- }
- private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
- listBox1->Items->Clear();
- }
- private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) {
- setFilePath();
- }
- private: System::Void button5_Click(System::Object^ sender, System::EventArgs^ e) {
- if (FILEPATH == "") setFilePath();
- auto begin = std::chrono::steady_clock::now();
- find_groups_cheating();
- auto end = std::chrono::steady_clock::now();
- auto elapsed_ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin);
- dh("поиск занял " + s(elapsed_ms.count()) + " ms");
- }
- private: System::Void button6_Click(System::Object^ sender, System::EventArgs^ e) {
- std::string word = s(textBox1->Text->ToUpper());
- if (FILEPATH == "") setFilePath();
- else if (word == "") dh("введи хоть что-нибудь");
- else {
- auto begin = std::chrono::steady_clock::now();
- auto result = find_with_cheats(word); // ПОИСК
- auto end = std::chrono::steady_clock::now();
- auto elapsed_ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin);
- dh("поиск занял " + s(elapsed_ms.count()) + " ms");
- if (result.size() > 1) {
- dh("найденные анаграммы для " + word + ":");
- for (auto el : result) if (el != word) dh(el);
- }
- else dh("анаграмм для " + word + " не найдено");
- }
- dh("");
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement