Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#pragma GCC optimize("03")
- //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
- #include <iostream>
- #include <iomanip>
- #include <cstdlib>
- #include <cstdio>
- #include <string>
- #include <vector>
- #include <map>
- #include <set>
- #include <unordered_map>
- #include <unordered_set>
- #include <queue>
- #include <deque>
- #include <cmath>
- #include <numeric>
- #include <algorithm>
- #include <ctime>
- #include <chrono>
- #include <random>
- #include <functional>
- #include <fstream>
- using namespace std;
- const int MOD = 1e9 + 7;
- const int xx = 257;
- vector<long long> x = {1};
- bool isequal(int from1, int from2, int len, vector<long long> h, vector<long long> x, vector<long long> h2) {
- return (h[from1 + len - 1] + h2[from2 - 1] * x[len]) % MOD ==
- (h2[from2 + len - 1] + h[from1 - 1] * x[len]) % MOD;
- }
- void make_x_koefs(int n) {
- int k = x.size();
- if (n + 1 > k) {
- x.resize(n + 1);
- }
- for (int i = k; i < n + 1; i++) {
- x[i] = (x[i - 1] * xx) % MOD;
- }
- }
- vector <long long> make_polynom(string s) {
- s = " " + s;
- vector <long long> h(s.size(), 0);
- for (int i = 1; i < s.size(); i++) {
- h[i] = (h[i - 1] * xx + s[i]) % MOD;
- }
- return h;
- }
- vector <string> read_tests(string fname) {
- fstream tests;
- tests.open(fname, ios::in);
- vector <string> ts;
- if (!tests.is_open()) {
- return ts;
- }
- string s;
- while (tests >> s) {
- ts.push_back(s);
- }
- tests.close();
- return ts;
- }
- string read_text(string fname) {
- fstream Text_to_check;
- Text_to_check.open(fname, ios::in);
- string text_s;
- if (!Text_to_check.is_open()) {
- return text_s;
- }
- string s;
- while (!Text_to_check.eof()) {
- getline(Text_to_check, s);
- text_s += s;
- if (!Text_to_check.eof()) {
- text_s += ' ';
- }
- }
- Text_to_check.close();
- return text_s;
- }
- int main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout << setprecision(2);
- cout << fixed;
- setlocale(LC_ALL, "RU");
- string cmd = "?";
- string message = "*****************************************\nСписок команд:\nquit - выход\nload_text - выбрать текст для проверки\n";
- message += "load_tests - загрузить тесты на плагиат\n? - справка\ncheck - проверить текст\n";
- message += "*****************************************\n";
- string text = "";
- vector<string> tests = {};
- string error = "Ошибка!\n";
- bool txt = false;
- bool tst = false;
- int N = 0;
- while (cmd != "quit") {
- if (cmd == "?") {
- cout << message;
- }
- else if (cmd == "load_text") {
- try {
- cout << "Введите имя файла с расширением: ";
- string x;
- cin >> x;
- text = read_text(x);
- if (text != "") {
- N = text.size();
- txt = true;
- }
- else {
- cout << "Файл пуст!\n";
- }
- }
- catch (...) {
- cout << error;
- }
- }
- else if (cmd == "load_tests") {
- try {
- cout << "Введите имя файла с расширением: ";
- string x;
- cin >> x;
- tests = read_tests(x);
- if (!tests.empty()) {
- tst = true;
- }
- else {
- cout << "Файл пуст!\n";
- }
- }
- catch (...) {
- cout << error;
- }
- }
- else if (cmd == "check") {
- if (txt and tst) {
- make_x_koefs(N + 1);
- vector <long long> h = make_polynom(text);
- long long len_same = 0;
- bool flag = true;
- vector<vector <long long>> testsll;
- for (auto now : tests) {
- if (now.size() > N) {
- cout << "Тест длиннее текста!\n";
- flag = false;
- break;
- }
- testsll.push_back(make_polynom(now));
- }
- text = " " + text;
- for (int i = 1; i < text.size(); i++) {
- for (int j = 0; j < testsll.size(); j++) {
- if (i + testsll[j].size() - 1 < text.size()) {
- if (isequal(i, 1, testsll[j].size() - 1, h, x, testsll[j])) {
- len_same += testsll[j].size() - 1;
- }
- }
- }
- }
- cout << "Процент плагиата:" << (long double)len_same / (text.size() - 1) * 100 << "%\n";
- }
- else {
- cout << "Не выбран текст или тесты!\n";
- }
- }
- else {
- cout << error;
- }
- cin >> cmd;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement