/
DeC2018
/
0032
Обзор
Документация
Войти
/
DeC2018
/
0032
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
main.cpp
47 строк
1 KB
Den
create main.cpp
08 янв 2025, 22:12
08 янв 2025, 22:12
3de1de8
Код
Авторство
О чём код?
#include <iostream> #include <stack> #include <string> using namespace std; class Solution { public: int longestValidParentheses(string s) { stack<int> st; st.push(-1); int max_len = 0; for (int i = 0; i < s.length(); i++) { if (s[i] == '(') { st.push(i); } else { st.pop(); if (st.empty()) { st.push(i); } else { max_len = max(max_len, i - st.top()); } } } return max_len; } }; int main() { Solution solution; string input; // Test case 1 input = "(()"; cout << "Input: " << input << ", Output: " << solution.longestValidParentheses(input) << endl; // Test case 2 input = ")()())"; cout << "Input: " << input << ", Output: " << solution.longestValidParentheses(input) << endl; // Test case 3 input = ""; cout << "Input: " << input << ", Output: " << solution.longestValidParentheses(input) << endl; return 0; }