Advertisement
cyberjab

Untitled

Dec 24th, 2023
968
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.23 KB | None | 0 0
  1. //#pragma GCC optimize("03")
  2. //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <cstdlib>
  6. #include <cstdio>
  7. #include <string>
  8. #include <vector>
  9. #include <map>
  10. #include <set>
  11. #include <unordered_map>
  12. #include <unordered_set>
  13. #include <queue>
  14. #include <deque>
  15. #include <cmath>
  16. #include <numeric>
  17. #include <algorithm>
  18. #include <ctime>
  19. #include <chrono>
  20. #include <random>
  21. #include <functional>
  22. #include <fstream>
  23.  
  24. using namespace std;
  25. const int MOD = 1e9 + 7;
  26. const int xx = 257;
  27.  
  28. vector<long long> x = {1};
  29.  
  30. bool isequal(int from1, int from2, int len, vector<long long> h, vector<long long> x, vector<long long> h2) {
  31.     return (h[from1 + len - 1] + h2[from2 - 1] * x[len]) % MOD ==
  32.         (h2[from2 + len - 1] + h[from1 - 1] * x[len]) % MOD;
  33. }
  34.  
  35. void make_x_koefs(int n) {
  36.     int k = x.size();
  37.     if (n + 1 > k) {
  38.         x.resize(n + 1);
  39.     }
  40.     for (int i = k; i < n + 1; i++) {
  41.         x[i] = (x[i - 1] * xx) % MOD;
  42.     }
  43. }
  44.  
  45. vector <long long> make_polynom(string s) {
  46.     s = " " + s;
  47.     vector <long long> h(s.size(), 0);
  48.     for (int i = 1; i < s.size(); i++) {
  49.         h[i] = (h[i - 1] * xx + s[i]) % MOD;
  50.     }
  51.     return h;
  52. }
  53.  
  54. vector <string> read_tests(string fname) {
  55.     fstream tests;
  56.     tests.open(fname, ios::in);
  57.     vector <string> ts;
  58.     if (!tests.is_open()) {
  59.         return ts;
  60.     }
  61.     string s;
  62.     while (tests >> s) {
  63.         ts.push_back(s);
  64.     }
  65.     tests.close();
  66.     return ts;
  67. }
  68.  
  69. string read_text(string fname) {
  70.     fstream Text_to_check;
  71.     Text_to_check.open(fname, ios::in);
  72.     string text_s;
  73.     if (!Text_to_check.is_open()) {
  74.         return text_s;
  75.     }
  76.     string s;
  77.     while (!Text_to_check.eof()) {
  78.         getline(Text_to_check, s);
  79.         text_s += s;
  80.         if (!Text_to_check.eof()) {
  81.             text_s += ' ';
  82.         }
  83.     }
  84.     Text_to_check.close();
  85.     return text_s;
  86. }
  87.  
  88. int main() {
  89.     ios_base::sync_with_stdio(0);
  90.     cin.tie(0);
  91.     cout << setprecision(2);
  92.     cout << fixed;
  93.     setlocale(LC_ALL, "RU");
  94.     string cmd = "?";
  95.     string message = "*****************************************\nСписок команд:\nquit - выход\nload_text - выбрать текст для проверки\n";
  96.     message += "load_tests - загрузить тесты на плагиат\n? - справка\ncheck - проверить текст\n";
  97.     message += "*****************************************\n";
  98.     string text = "";
  99.     vector<string> tests = {};
  100.     string error = "Ошибка!\n";
  101.     bool txt = false;
  102.     bool tst = false;
  103.     int N = 0;
  104.     while (cmd != "quit") {
  105.         if (cmd == "?") {
  106.             cout << message;
  107.         }
  108.         else if (cmd == "load_text") {
  109.             try {
  110.                 cout << "Введите имя файла с расширением: ";
  111.                 string x;
  112.                 cin >> x;
  113.                 text = read_text(x);
  114.                 if (text != "") {
  115.                     N = text.size();
  116.                     txt = true;
  117.                 }
  118.                 else {
  119.                     cout << "Файл пуст!\n";
  120.                 }
  121.             }
  122.             catch (...) {
  123.                 cout << error;
  124.             }
  125.         }
  126.         else if (cmd == "load_tests") {
  127.             try {
  128.                 cout << "Введите имя файла с расширением: ";
  129.                 string x;
  130.                 cin >> x;
  131.                 tests = read_tests(x);
  132.                 if (!tests.empty()) {
  133.                     tst = true;
  134.                 }
  135.                 else {
  136.                     cout << "Файл пуст!\n";
  137.                 }
  138.             }
  139.             catch (...) {
  140.                 cout << error;
  141.             }
  142.         }
  143.         else if (cmd == "check") {
  144.             if (txt and tst) {
  145.                 make_x_koefs(N + 1);
  146.                 vector <long long> h = make_polynom(text);
  147.                 long long len_same = 0;
  148.                 bool flag = true;
  149.                 vector<vector <long long>> testsll;
  150.                 for (auto now : tests) {
  151.                     if (now.size() > N) {
  152.                         cout << "Тест длиннее текста!\n";
  153.                         flag = false;
  154.                         break;
  155.                     }
  156.                     testsll.push_back(make_polynom(now));
  157.                 }
  158.                 text = " " + text;
  159.                 for (int i = 1; i < text.size(); i++) {
  160.                     for (int j = 0; j < testsll.size(); j++) {
  161.                         if (i + testsll[j].size() - 1 < text.size()) {
  162.                             if (isequal(i, 1, testsll[j].size() - 1, h, x, testsll[j])) {
  163.                                 len_same += testsll[j].size() - 1;
  164.                             }
  165.                         }
  166.                     }
  167.                 }
  168.                 cout << "Процент плагиата:" << (long double)len_same / (text.size() - 1) * 100 << "%\n";
  169.             }
  170.             else {
  171.                 cout << "Не выбран текст или тесты!\n";
  172.             }
  173.         }
  174.         else {
  175.             cout << error;
  176.         }
  177.         cin >> cmd;
  178.     }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement