#include <iostream> using namespace std; int main() { int i, n, k, counter = 0, wordsCounter = 0; bool noClosedBracket = false; bool addOne = false; bool isWord = false; cin >> n; cin >> k; int podzial = n / k; if (podzial < 2) { cout << "0"; return 0; } k -= 1; char* tab = new char[n]; int* div = new int[n]; for (i = 0; i < n; i++) { cin >> tab[i]; } for (int i = 0; i < n; i++) { counter++; if (i == n - 1) { podzial -= 2; } if (counter == podzial && k > 0) { div[i] = 1; counter = 0; k--; } else { div[i] = 0; } } for (i = 0; i < n - 1; i++) { if (isWord == true && tab[i] == ')' && noClosedBracket == false) { isWord = false; } if (noClosedBracket && tab[i] == ')' && div[i] == 0) { wordsCounter++; if (isWord) { wordsCounter++; isWord = false; } else { isWord = true; } noClosedBracket = false; } else { if (tab[i] == '(' && tab[i + 1] == ')' && div[i] == 0 && div[i + 1] == 0) { wordsCounter++; if (isWord) { wordsCounter++; isWord = false; } else { isWord = true; } i++; } else if (div[i] == 1) { noClosedBracket = false; addOne = false; isWord = false; } else if (div[i] == 0 && tab[i] == '(') { noClosedBracket = true; if (isWord) { addOne = true; } } } } if (noClosedBracket && tab[n] == ')') { wordsCounter++; isWord = true; } if (addOne) { wordsCounter++; } cout << wordsCounter; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | #include <iostream> using namespace std; int main() { int i, n, k, counter = 0, wordsCounter = 0; bool noClosedBracket = false; bool addOne = false; bool isWord = false; cin >> n; cin >> k; int podzial = n / k; if (podzial < 2) { cout << "0"; return 0; } k -= 1; char* tab = new char[n]; int* div = new int[n]; for (i = 0; i < n; i++) { cin >> tab[i]; } for (int i = 0; i < n; i++) { counter++; if (i == n - 1) { podzial -= 2; } if (counter == podzial && k > 0) { div[i] = 1; counter = 0; k--; } else { div[i] = 0; } } for (i = 0; i < n - 1; i++) { if (isWord == true && tab[i] == ')' && noClosedBracket == false) { isWord = false; } if (noClosedBracket && tab[i] == ')' && div[i] == 0) { wordsCounter++; if (isWord) { wordsCounter++; isWord = false; } else { isWord = true; } noClosedBracket = false; } else { if (tab[i] == '(' && tab[i + 1] == ')' && div[i] == 0 && div[i + 1] == 0) { wordsCounter++; if (isWord) { wordsCounter++; isWord = false; } else { isWord = true; } i++; } else if (div[i] == 1) { noClosedBracket = false; addOne = false; isWord = false; } else if (div[i] == 0 && tab[i] == '(') { noClosedBracket = true; if (isWord) { addOne = true; } } } } if (noClosedBracket && tab[n] == ')') { wordsCounter++; isWord = true; } if (addOne) { wordsCounter++; } cout << wordsCounter; } |