Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- #include <stdlib.h>
- using namespace std;
- //Основная задача - реализовать данный метод
- //Можно изменить передачу параметров на ссылки (&)
- //Можно добавлять любое количество любых вспомогательных методов, структур и классов
- void getSubstrings(string& source, string& substring, vector<int>& res)
- {
- int n = substring.length(), i, t, position = 0;
- int* borderArray = new int[n];
- borderArray[0] = 0;
- for (i = 1; i < n; i++) {
- t = borderArray[i - 1];
- while ((t > 0) && (substring[i] != substring[t]))
- t = borderArray[t - 1];
- if (substring[i] == substring[t])
- borderArray[i] = t + 1;
- else
- borderArray[i] = 0;
- }
- for (int i = 0; i < source.size(); i++)
- {
- while (position == substring.size() || (position > 0 && substring[position] != source[i]))
- {
- position = borderArray[position - 1];
- if (substring.size() - position > source.size() - i)
- break;
- }
- if (source[i] == substring[position])
- position++;
- if (position == substring.size())
- res.push_back(i - position + 1);
- }
- delete[] borderArray;
- }
- //Не изменять метод main без крайней необходимости
- //ОБЯЗАТЕЛЬНО добавить в комментариях подробные пояснения и причины побудившие вас изменить код этого метода.
- int main()
- {
- string t;
- string p;
- vector<int> res;
- ifstream fin;
- fin.open("input.txt");
- if (fin.is_open())
- {
- getline(fin, t);
- getline(fin, p);
- fin.close();
- }
- getSubstrings(t, p, res);
- fstream fout;
- fout.open("output.txt", ios::out);
- fout << res.size() << "\n";
- for (std::vector<int>::const_iterator i = res.begin(); i != res.end(); ++i)
- fout << *i << "\n";
- fout.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement