Advertisement
neongm

Untitled

Feb 26th, 2021
1,164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.71 KB | None | 0 0
  1. #pragma once
  2. #include <msclr\marshal_cppstd.h>
  3. #include <algorithm>
  4. #include <fstream>
  5. #include <string>
  6. #include <set>
  7. #include <windows.h>
  8. #include <chrono>
  9. #include <map>
  10.  
  11. template<class T>
  12. struct equal : std::binary_function<T, T, bool> {
  13.     bool operator () (const T& a, const T& b) { return a == b; }
  14. };
  15.  
  16. namespace ANAGRAM {
  17.  
  18.     using namespace System;
  19.     using namespace System::ComponentModel;
  20.     using namespace System::Collections;
  21.     using namespace System::Windows::Forms;
  22.     using namespace System::Data;
  23.     using namespace System::Drawing;
  24.  
  25.     /// <summary>
  26.     /// Сводка для MyForm
  27.     /// </summary>
  28.    
  29.     std::string FILEPATH = "";
  30.    
  31.     public ref class MyForm : public System::Windows::Forms::Form
  32.     {
  33.     public:
  34.         MyForm(void)
  35.         {
  36.             InitializeComponent();
  37.             //
  38.             //дTODO: добавьте код конструктора
  39.             //
  40.         }
  41.  
  42.     protected:
  43.         /// <summary>
  44.         /// Освободить все используемые ресурсы.
  45.         /// </summary>
  46.         ~MyForm()
  47.         {
  48.             if (components)
  49.             {
  50.                 delete components;
  51.             }
  52.         }
  53.     private: System::Windows::Forms::Button^ button1;
  54.     private: System::Windows::Forms::ListBox^ listBox1;
  55.     private: System::Windows::Forms::TextBox^ textBox1;
  56.     private: System::Windows::Forms::Button^ button2;
  57.     private: System::Windows::Forms::Label^ label1;
  58.     private: System::Windows::Forms::Button^ button3;
  59.     private: System::Windows::Forms::Button^ button4;
  60.     private: System::Windows::Forms::OpenFileDialog^ openFileDialog1;
  61.     private: System::Windows::Forms::Button^ button5;
  62.     private: System::Windows::Forms::Button^ button6;
  63.     protected:
  64.  
  65.     private:
  66.         /// <summary>
  67.         /// Обязательная переменная конструктора.
  68.         /// </summary>
  69.         System::ComponentModel::Container ^components;
  70.  
  71. #pragma region Windows Form Designer generated code
  72.         /// <summary>
  73.         /// Требуемый метод для поддержки конструктора — не изменяйте
  74.         /// содержимое этого метода с помощью редактора кода.
  75.         /// </summary>
  76.         void InitializeComponent(void)
  77.         {
  78.             this->button1 = (gcnew System::Windows::Forms::Button());
  79.             this->listBox1 = (gcnew System::Windows::Forms::ListBox());
  80.             this->textBox1 = (gcnew System::Windows::Forms::TextBox());
  81.             this->button2 = (gcnew System::Windows::Forms::Button());
  82.             this->label1 = (gcnew System::Windows::Forms::Label());
  83.             this->button3 = (gcnew System::Windows::Forms::Button());
  84.             this->button4 = (gcnew System::Windows::Forms::Button());
  85.             this->openFileDialog1 = (gcnew System::Windows::Forms::OpenFileDialog());
  86.             this->button5 = (gcnew System::Windows::Forms::Button());
  87.             this->button6 = (gcnew System::Windows::Forms::Button());
  88.             this->SuspendLayout();
  89.             //
  90.             // button1
  91.             //
  92.             this->button1->Location = System::Drawing::Point(12, 43);
  93.             this->button1->Name = L"button1";
  94.             this->button1->Size = System::Drawing::Size(75, 23);
  95.             this->button1->TabIndex = 0;
  96.             this->button1->Text = L"1й вариант";
  97.             this->button1->UseVisualStyleBackColor = true;
  98.             this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click);
  99.             //
  100.             // listBox1
  101.             //
  102.             this->listBox1->FormattingEnabled = true;
  103.             this->listBox1->Location = System::Drawing::Point(232, 17);
  104.             this->listBox1->Name = L"listBox1";
  105.             this->listBox1->Size = System::Drawing::Size(297, 238);
  106.             this->listBox1->TabIndex = 1;
  107.             //
  108.             // textBox1
  109.             //
  110.             this->textBox1->Location = System::Drawing::Point(122, 17);
  111.             this->textBox1->Name = L"textBox1";
  112.             this->textBox1->Size = System::Drawing::Size(104, 20);
  113.             this->textBox1->TabIndex = 2;
  114.             //
  115.             // button2
  116.             //
  117.             this->button2->Location = System::Drawing::Point(12, 72);
  118.             this->button2->Name = L"button2";
  119.             this->button2->Size = System::Drawing::Size(214, 23);
  120.             this->button2->TabIndex = 3;
  121.             this->button2->Text = L"Поиск всех групп анаграмм";
  122.             this->button2->UseVisualStyleBackColor = true;
  123.             this->button2->Click += gcnew System::EventHandler(this, &MyForm::button2_Click);
  124.             //
  125.             // label1
  126.             //
  127.             this->label1->AutoSize = true;
  128.             this->label1->Location = System::Drawing::Point(13, 20);
  129.             this->label1->Name = L"label1";
  130.             this->label1->Size = System::Drawing::Size(103, 13);
  131.             this->label1->TabIndex = 4;
  132.             this->label1->Text = L"слово для поиска: ";
  133.             //
  134.             // button3
  135.             //
  136.             this->button3->Location = System::Drawing::Point(232, 261);
  137.             this->button3->Name = L"button3";
  138.             this->button3->Size = System::Drawing::Size(297, 23);
  139.             this->button3->TabIndex = 5;
  140.             this->button3->Text = L"clear listbox";
  141.             this->button3->UseVisualStyleBackColor = true;
  142.             this->button3->Click += gcnew System::EventHandler(this, &MyForm::button3_Click);
  143.             //
  144.             // button4
  145.             //
  146.             this->button4->Location = System::Drawing::Point(12, 260);
  147.             this->button4->Name = L"button4";
  148.             this->button4->Size = System::Drawing::Size(75, 23);
  149.             this->button4->TabIndex = 6;
  150.             this->button4->Text = L"select file";
  151.             this->button4->UseVisualStyleBackColor = true;
  152.             this->button4->Click += gcnew System::EventHandler(this, &MyForm::button4_Click);
  153.             //
  154.             // openFileDialog1
  155.             //
  156.             this->openFileDialog1->FileName = L"openFileDialog1";
  157.             //
  158.             // button5
  159.             //
  160.             this->button5->Location = System::Drawing::Point(12, 101);
  161.             this->button5->Name = L"button5";
  162.             this->button5->Size = System::Drawing::Size(214, 23);
  163.             this->button5->TabIndex = 7;
  164.             this->button5->Text = L"то же, что выше, но сильно быстрее";
  165.             this->button5->UseVisualStyleBackColor = true;
  166.             this->button5->Click += gcnew System::EventHandler(this, &MyForm::button5_Click);
  167.             //
  168.             // button6
  169.             //
  170.             this->button6->Location = System::Drawing::Point(93, 43);
  171.             this->button6->Name = L"button6";
  172.             this->button6->Size = System::Drawing::Size(133, 23);
  173.             this->button6->TabIndex = 8;
  174.             this->button6->Text = L"он же, но чуть быстрее";
  175.             this->button6->UseVisualStyleBackColor = true;
  176.             this->button6->Click += gcnew System::EventHandler(this, &MyForm::button6_Click);
  177.             //
  178.             // MyForm
  179.             //
  180.             this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
  181.             this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
  182.             this->ClientSize = System::Drawing::Size(547, 301);
  183.             this->Controls->Add(this->button6);
  184.             this->Controls->Add(this->button5);
  185.             this->Controls->Add(this->button4);
  186.             this->Controls->Add(this->button3);
  187.             this->Controls->Add(this->label1);
  188.             this->Controls->Add(this->button2);
  189.             this->Controls->Add(this->textBox1);
  190.             this->Controls->Add(this->listBox1);
  191.             this->Controls->Add(this->button1);
  192.             this->MaximizeBox = false;
  193.             this->MaximumSize = System::Drawing::Size(563, 340);
  194.             this->MinimumSize = System::Drawing::Size(563, 340);
  195.             this->Name = L"MyForm";
  196.             this->Text = L"анаграмматор 300";
  197.             this->ResumeLayout(false);
  198.             this->PerformLayout();
  199.  
  200.         }
  201. #pragma endregion
  202.         void dh(std::string message) {
  203.             listBox1->Items->Insert(listBox1->Items->Count, cs(message));
  204.         }void dh(String^ message) {
  205.             listBox1->Items->Insert(listBox1->Items->Count, message);
  206.         }
  207.        
  208.         // converts System::String to std::string
  209.         std::string s(String^ str) {
  210.             return msclr::interop::marshal_as<std::string>(str);
  211.         }std::string s(System::Decimal dec) {
  212.             return std::to_string(decToInt(dec));
  213.         }template<typename T>
  214.             std::string s(const T& value) {  // converts ints, floats and anything it can to std::string
  215.             return std::to_string(value);
  216.         }
  217.         // converts std::string to System::String^
  218.         System::String^ cs(std::string str) {
  219.             return gcnew String(str.c_str());
  220.         }
  221.         int decToInt(const System::Decimal& dec) {
  222.             return dec.ToInt32(dec); // работает, лол
  223.         }
  224.  
  225.         bool is_anagram(const std::string& word1, const std::string& word2) {
  226.             if (word1.size() == word2.size()) {
  227.                 std::multiset<char> wrd{ word1.begin(), word1.end() };
  228.                 std::multiset<char> buf{ word2.begin(), word2.end() };
  229.                 if (buf == wrd) { return true; };
  230.             }
  231.             else return false;
  232.         }
  233.  
  234.  
  235.         std::vector<std::string> find(std::string word) {
  236.             /// <summary>
  237.             /// simple straight-forward search algorithm working with
  238.             /// any type of files and utilizing multisets
  239.             /// </summary>
  240.             /// <param name="word"></param>
  241.             /// <returns>std::vector<std::string></returns>
  242.            
  243.             std::vector<std::string> found_anagrams;
  244.             std::ifstream file(FILEPATH);
  245.             std::string buffer;
  246.             std::multiset<char> wrd{ word.begin(), word.end() };
  247.  
  248.             if (!file.is_open()) { dh("file not opened"); }
  249.             else {     // !file.eof() instead of just while(file)
  250.                 while (!file.eof()) {
  251.                     getline(file, buffer);
  252.                     if (buffer.size() == word.size()) {
  253.                         std::multiset<char> buf{ buffer.begin(), buffer.end() };
  254.                         if (buf == wrd) { if(std::find(found_anagrams.begin(), found_anagrams.end(), buffer) != std::end(found_anagrams)) found_anagrams.push_back(buffer); };
  255.                     }
  256.                 }
  257.             }
  258.             file.close();
  259.             return found_anagrams;
  260.         }
  261.  
  262.         std::vector<std::string> find_with_cheats(std::string word) {
  263.             std::vector<std::string> found_anagrams;
  264.             std::ifstream file(FILEPATH);
  265.             std::string buffer;
  266.             std::multiset<char> wrd{ word.begin(), word.end() };
  267.             if (!file.is_open()) { dh("file not opened"); }
  268.             else {
  269.                 while (!file.eof()) {
  270.                     getline(file, buffer);
  271.                     if (buffer.size() == word.size()) {
  272.                         std::multiset<char> buf{ buffer.begin(), buffer.end() };
  273.                         if (buf == wrd) { found_anagrams.push_back(buffer); }
  274.                         else if (found_anagrams.size() != 0 && buf != wrd) {
  275.                             file.close();
  276.                             break;
  277.                         }
  278.                     }
  279.                 }
  280.             }
  281.             return found_anagrams;
  282.         }
  283.  
  284.         void setFilePath() {
  285.             openFileDialog1->Filter = "txt files (*.txt)|*.txt";
  286.             if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK) {
  287.                 FILEPATH = s(openFileDialog1->FileName);
  288.                 dh(FILEPATH);
  289.             }
  290.             else { dh("unable to add file path"); }
  291.         }
  292.  
  293.         std::vector<std::vector<std::string>> find_groups(uint8_t minimum_group_size_limit) {        // честный поиск
  294.             std::vector<std::vector<std::string>> groups_vector;
  295.             std::string buffer;
  296.             dh("opening file at "+FILEPATH);
  297.             std::ifstream file(FILEPATH);
  298.             std::vector<std::multiset<char>> multisets_vector;
  299.             if (!file.is_open()) { dh("unable to access file"); }
  300.             else {
  301.                     while (!file.eof()) {
  302.                     getline(file, buffer);
  303.                     std::multiset<char> buf{ buffer.begin(), buffer.end() };
  304.                     if(std::find(multisets_vector.begin(), multisets_vector.end(), buf) == multisets_vector.end()){
  305.                         auto found = find(buffer);
  306.                         if (found.size() > minimum_group_size_limit) {
  307.                             groups_vector.push_back(found);
  308.                             multisets_vector.push_back(buf);
  309.                         }  
  310.                     }
  311.                 }
  312.             }
  313.             return groups_vector;
  314.         }
  315.  
  316.         void find_groups_cheating() {           // читерский поиск
  317.             std::string buffer;
  318.             dh("opening file at " + FILEPATH);
  319.             std::multiset<char> prev_set;
  320.             std::ifstream file(FILEPATH);
  321.             int c = 0;
  322.             do {
  323.                 getline(file, buffer);
  324.                 std::multiset<char> buf{ buffer.begin(), buffer.end() };
  325.                 if(buf != prev_set){
  326.                     dh("----------next group: " + s(c++));
  327.                     prev_set = buf;
  328.                     dh(buffer);
  329.                 }
  330.                 else{ dh(buffer); }
  331.             } while (!file.eof());
  332.             file.close();
  333.             dh("готово");
  334.         }
  335.  
  336.  
  337.         void find_all(std::string word) {
  338.             dh("все возможные анаграммы cлова "+word+":");
  339.             std::sort(word.begin(), word.end());
  340.             do {
  341.                 dh(word);
  342.             } while (std::next_permutation(word.begin(), word.end()));
  343.         }
  344.        
  345.  
  346.  
  347.  
  348.     private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
  349.         std::string word = s(textBox1->Text->ToUpper());
  350.         if (FILEPATH == "") setFilePath();
  351.         else if (word == "") dh("введи хоть что-нибудь");
  352.         else {
  353.             auto begin = std::chrono::steady_clock::now();
  354.             auto result = find(word);  // ПОИСК
  355.             auto end = std::chrono::steady_clock::now();
  356.             auto elapsed_ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin);
  357.             dh("поиск занял "+s(elapsed_ms.count())+" ms");
  358.             if (result.size() != 1) {
  359.                 dh("найденные анаграммы для " + word + ":");
  360.                 for (auto el : result) if(el != word) dh(el);
  361.             }
  362.             else dh("анаграмм для "+word+" не найдено");
  363.         }
  364.     }
  365.  
  366.     private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
  367.         if (FILEPATH == "") setFilePath();
  368.         System::Windows::Forms::MessageBox ^box;
  369.         if (box->Show("Чумба, ты действительно хочешь использовать этот метод? это займет вечность", "ты серьезно?", System::Windows::Forms::MessageBoxButtons::YesNo, MessageBoxIcon::Question) == System::Windows::Forms::DialogResult::Yes) {
  370.             auto begin = std::chrono::steady_clock::now();
  371.             auto groups = find_groups(1); // ПОИСК
  372.             auto end = std::chrono::steady_clock::now();
  373.             auto elapsed_ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin);
  374.             dh("поиск занял " + s(elapsed_ms.count()) + " ms");
  375.             dh("найдено групп:" + s(groups.size()-1));
  376.             for (auto el : groups) {
  377.                 dh("----------next group:");
  378.                 for (auto str : el) {
  379.                     dh(str);
  380.                 }
  381.             }
  382.         }
  383.     }
  384.     private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
  385.         listBox1->Items->Clear();
  386.     }
  387.  
  388. private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) {
  389.     setFilePath();
  390. }
  391. private: System::Void button5_Click(System::Object^ sender, System::EventArgs^ e) {
  392.     if (FILEPATH == "") setFilePath();
  393.     auto begin = std::chrono::steady_clock::now();
  394.     find_groups_cheating();
  395.     auto end = std::chrono::steady_clock::now();
  396.     auto elapsed_ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin);
  397.     dh("поиск занял " + s(elapsed_ms.count()) + " ms");
  398. }
  399. private: System::Void button6_Click(System::Object^ sender, System::EventArgs^ e) {
  400.     std::string word = s(textBox1->Text->ToUpper());
  401.     if (FILEPATH == "") setFilePath();
  402.     else if (word == "") dh("введи хоть что-нибудь");
  403.     else {
  404.             auto begin = std::chrono::steady_clock::now();
  405.             auto result = find_with_cheats(word);  // ПОИСК
  406.             auto end = std::chrono::steady_clock::now();
  407.             auto elapsed_ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin);
  408.             dh("поиск занял " + s(elapsed_ms.count()) + " ms");
  409.             if (result.size() > 1) {
  410.                 dh("найденные анаграммы для " + word + ":");
  411.                 for (auto el : result)  if (el != word) dh(el);
  412.             }
  413.             else dh("анаграмм для " + word + " не найдено");
  414.     }
  415.     dh("");
  416. }
  417. };
  418. }
  419.  
  420.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement