답안 #604438

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
604438 2022-07-25T06:29:01 Z Aleee 괄호 문자열 (CEOI16_match) C++14
10 / 100
2000 ms 3300 KB
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define all(a) (a).begin(), (a).end()
#define endl '\n'
#define forn(i, l, n) for (int i = l; i < int(n); i++)

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;

const int INF = 1e9 + 7;
string solve(string str, int pos, int test, string ans, stack<char> st)
{
  if (pos >= str.length() && test == 0)
    return ans;
  if (pos >= str.length())
    return "";

  if (test < 0)
    return "";
  if (st.empty())
  {
    ans += "(";
    st.push(str[pos]);
    test++;
    return solve(str, pos + 1, test, ans, st);
  }
  if (st.top() != str[pos])
  {
    ans += "(";
    st.push(str[pos]);
    test++;
    return solve(str, pos + 1, test, ans, st);
  }
  else
  {
    st.push(str[pos]);
    string a = solve(str, pos + 1, test + 1, ans + "(", st);
    st.pop();
    st.pop();
    string b = solve(str, pos + 1, test - 1, ans + ")", st);
    if (a.size() || b.size())
    {
      if (a.size())
        return a;
      else
        return b;
    }
    else
      return "";
  }
}
int main() // Ale C.
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // freopen("output.txt", "w", stdout);
  string str;
  cin >> str;
  string ans = solve(str, 0, 0, "", stack<char>());
  cout << (ans.length() ? ans : "-1");
  return 0;
}

Compilation message

match.cpp: In function 'std::string solve(std::string, int, int, std::string, std::stack<char>)':
match.cpp:17:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   17 |   if (pos >= str.length() && test == 0)
      |       ~~~~^~~~~~~~~~~~~~~
match.cpp:19:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   19 |   if (pos >= str.length())
      |       ~~~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 2 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 2 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Execution timed out 2068 ms 3300 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 2 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Execution timed out 2068 ms 3300 KB Time limit exceeded
5 Halted 0 ms 0 KB -