prestige

ShahnovskiyLab2

Oct 29th, 2012
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <Windows.h>
  5. #include <map>
  6. #include <vector>
  7. #include <algorithm>
  8. using namespace std;
  9.  
  10. string path, virus_mask;
  11. string path1, path2;
  12. ifstream in("replacement.txt");
  13. vector<pair<string, string> > exchange;
  14. vector<int> z_function(const string &s) {
  15.     int n = (int)s.length();
  16.     vector<int> z(n, 0);
  17.     for (int i = 1, l = 0, r = 0; i < n; i++) {
  18.         if(i <= r)
  19.             z[i] = min(z[i - l], r - i + 1);
  20.         while(z[i] + i < n && s[z[i]] == s[i + z[i]])
  21.             ++z[i];
  22.         if(i  + z[i] - 1 > r)
  23.             l = i, r = i + z[i] - 1;
  24.     }
  25.     return z;
  26. }
  27. vector<pair< pair<int, int>, pair<int, int> > > answer; // от куда до куда заменять в какой строке чем
  28.  
  29. bool comp(pair<string, string> a, pair<string, string> b) {
  30.     if(a.first.length() > b.first.length())
  31.         return true;
  32.     if(a.first.length() == b.first.length() && a.first < b.first)
  33.         return true;
  34.     return false;
  35.  
  36. }
  37. bool isVirus(const string &s) {
  38.     vector<int> z = z_function(virus_mask + '#' + s);
  39.     int n = z.size();
  40.     for (int i = 0; i < n; i++) {
  41.         if (z[i] == virus_mask.length()) {
  42.             return z[i];
  43.         }
  44.     }
  45.     return z[n - 1] == virus_mask.length();
  46. }
  47.  
  48. int main() {
  49.     string s;
  50.     while(getline(in, s)) {
  51.         string pattern, replacement;
  52.         bool f = 0;
  53.         for (int i = 0; i < s.length(); i++) {
  54.             if(s[i] == ' ' ) {
  55.                 f = 1;
  56.                 continue;
  57.             }
  58.             if(f)
  59.                 replacement.push_back(s[i]);
  60.             else
  61.                 pattern.push_back(s[i]);
  62.         }
  63.         exchange.push_back(make_pair(pattern, replacement));
  64.     }
  65.  
  66.     sort(exchange.begin(), exchange.end(), comp);
  67.     in.close();
  68.     in.open("text.txt", ifstream::in);
  69.     s = "";
  70.     int k = 0;
  71.     while(getline(in, s)) {
  72.         int len = s.length();
  73.         for (int i = 0; i < len; i++) {
  74.             int sz = exchange.size();
  75.             for (int j = 0; j < sz; j++) {
  76.                 virus_mask = exchange[j].first;
  77.                 int t1 = virus_mask.length();
  78.                 if(isVirus(s.substr(i, i + t1))) {
  79.                     answer.push_back(make_pair(make_pair(i, i + t1),make_pair(k, j)));
  80.                     i += t1;
  81.                 }
  82.             }
  83.         }
  84.         k++;
  85.     }
  86.     in.close();
  87.     in.open("text.txt", ifstream::in);
  88.     ofstream out("result.txt");
  89.     k = 0;
  90.     int i = 0;
  91.     int shift = 0;
  92.     while(getline(in, s)) {
  93.         int len = s.length();
  94.         for (int j = 0; j < len; j++) {
  95.             /*for (; i < answer.size() && answer[i].second.first == k; i++) {
  96.                 if
  97.             }*/
  98.             if(answer[i].second.first == k && answer[i].first.first == i) {
  99.                 j += answer[i].first.first;
  100.                 out << exchange[answer[i].second.second].second;
  101.                 i++;
  102.             }
  103.             else out << s[j];
  104.         }
  105.         k++;
  106.         out << endl;
  107.     }
  108.     return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment