Advertisement
smatskevich

Seminar7

Jan 13th, 2022
890
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.15 KB | None | 0 0
  1. #include <algorithm>
  2. #include <cmath>
  3. #include <iostream>
  4. #include <map>
  5. #include <set>
  6. #include <string>
  7. #include <tuple>
  8. #include <unordered_map>
  9. #include <unordered_set>
  10. #include <vector>
  11.  
  12. typedef long long ll;
  13. using namespace std;
  14.  
  15. // Разворот C-строки
  16. int main1() {
  17.   ios::sync_with_stdio(false);
  18.   cin.tie(nullptr);
  19.  
  20.   // Готовим C-шную строку.
  21.   char* s = new char[1000];
  22.   int i = 0;
  23.   for (char c = 0; c = char(getchar()), c != '\n'; ++i) {
  24.     s[i] = c;
  25.   }
  26.   s[i] = '\0';
  27.  
  28.   // Развернем строку.
  29.   std::reverse(s, s + i);
  30.  
  31.   cout << s;
  32.   delete[] s;
  33.   return 0;
  34. }
  35.  
  36. // Разворот строки std::string
  37. int main2() {
  38.   ios::sync_with_stdio(false);
  39.   cin.tie(nullptr);
  40.  
  41.   string s;
  42.   cin >> s;
  43.  
  44.   // Развернем строку.
  45.   std::reverse(s.begin(), s.end());
  46.   cout << s;
  47.   return 0;
  48. }
  49.  
  50. // Генерация палиндрома
  51. int main4() {
  52.   ios::sync_with_stdio(false);
  53.   cin.tie(nullptr);
  54.  
  55.   string s;
  56.   cin >> s;
  57.   cout << s;
  58.   reverse(s.begin(), s.end());
  59.   cout << s;
  60.   return 0;
  61. }
  62.  
  63. // Сложение всех чисел, входящих в строку.
  64. int main5() {
  65.   ios::sync_with_stdio(false);
  66.   cin.tie(nullptr);
  67.  
  68.   string s;
  69.   cin >> s;
  70.   // Можно добавить символ, чтобы не добавлять current_num после основного цикла.
  71.   // s.append(1, '*');
  72.  
  73.   // 2j3
  74.   // a3874dj33de4
  75.   int current_num = 0;
  76.   int result = 0;
  77.   for (char c : s) {
  78.     if (c >= '0' && c <= '9') {
  79.       current_num = current_num * 10 + (c - '0');
  80.     } else {
  81.       result += current_num;
  82.       current_num = 0;
  83.     }
  84.   }
  85.   result += current_num;
  86.  
  87.   cout << result;
  88.   return 0;
  89. }
  90.  
  91. // Префикс-функция
  92. int main() {
  93.   ios::sync_with_stdio(false);
  94.   cin.tie(nullptr);
  95.  
  96.   string s;
  97.   cin >> s;
  98.   vector<int> pi(s.size());
  99.   int j = 0;
  100.   for (size_t i = 1; i < s.size(); i++) {
  101.     while (j > 0 && s[j] != s[i]) j = pi[j - 1];
  102.     if (s[j] == s[i]) pi[i] = ++j;
  103.     // else оставляем pi[i] равным 0
  104.   }
  105.  
  106.   for (int i : pi) cout << i << " ";
  107.   return 0;
  108. }
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement