Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <Windows.h>
- #include <map>
- #include <vector>
- #include <algorithm>
- using namespace std;
- string path, virus_mask;
- string path1, path2;
- ifstream in("replacement.txt");
- vector<pair<string, string> > exchange;
- vector<int> z_function(const string &s) {
- int n = (int)s.length();
- vector<int> z(n, 0);
- for (int i = 1, l = 0, r = 0; i < n; i++) {
- if(i <= r)
- z[i] = min(z[i - l], r - i + 1);
- while(z[i] + i < n && s[z[i]] == s[i + z[i]])
- ++z[i];
- if(i + z[i] - 1 > r)
- l = i, r = i + z[i] - 1;
- }
- return z;
- }
- vector<pair< pair<int, int>, pair<int, int> > > answer; // от куда до куда заменять в какой строке чем
- bool comp(pair<string, string> a, pair<string, string> b) {
- if(a.first.length() > b.first.length())
- return true;
- if(a.first.length() == b.first.length() && a.first < b.first)
- return true;
- return false;
- }
- bool isVirus(const string &s) {
- vector<int> z = z_function(virus_mask + '#' + s);
- int n = z.size();
- for (int i = 0; i < n; i++) {
- if (z[i] == virus_mask.length()) {
- return z[i];
- }
- }
- return z[n - 1] == virus_mask.length();
- }
- int main() {
- string s;
- while(getline(in, s)) {
- string pattern, replacement;
- bool f = 0;
- for (int i = 0; i < s.length(); i++) {
- if(s[i] == ' ' ) {
- f = 1;
- continue;
- }
- if(f)
- replacement.push_back(s[i]);
- else
- pattern.push_back(s[i]);
- }
- exchange.push_back(make_pair(pattern, replacement));
- }
- sort(exchange.begin(), exchange.end(), comp);
- in.close();
- in.open("text.txt", ifstream::in);
- s = "";
- int k = 0;
- while(getline(in, s)) {
- int len = s.length();
- for (int i = 0; i < len; i++) {
- int sz = exchange.size();
- for (int j = 0; j < sz; j++) {
- virus_mask = exchange[j].first;
- int t1 = virus_mask.length();
- string str = s.substr(i, t1);
- if(isVirus(str)) {
- answer.push_back(make_pair(make_pair(i, i + t1),make_pair(k, j)));
- i += (t1 - 1);
- }
- }
- }
- k++;
- }
- in.close();
- in.open("text.txt", ifstream::in);
- ofstream out("result.txt");
- k = 0;
- int i = 0;
- while(getline(in, s)) {
- int len = s.length();
- for (int j = 0; j < len; j++) {
- if(i < answer.size() && answer[i].second.first == k && answer[i].first.first == j) {
- j += exchange[answer[i].second.second].first.length();
- out << exchange[answer[i].second.second].second;
- i++;
- j--;
- }
- else
- out << s[j];
- }
- k++;
- out << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment