Home
Users
Problems
Contests
Posts
Submissions
FAQ
❤️
Donate
Login
Register
|
Matcom Online Grader
View on GitHub
Join our community
Id
Date
User
Problem
Result
Time
Mem
Lang
830
15/05/2013 22:50:08
leandro
E - Odio los paréntesis
Accepted
48
412.0 KiB
GNU G++ 5.1.0
Code
/* username: leandro program: Odio los par�ntesis lang: C++ */ #include <stack> #include <string> #include <vector> #include <iostream> #include <algorithm> using namespace std; int test; string line; stack <char> S; vector <string> ret; vector < vector<string> > V; string c2s( char c ) { string ret = ""; ret = ret + c; return ret; } int prio( char c ) { switch ( c ) { case ')' : return 0; case '(' : return 1; case '+' : return 2; case '*' : return 3; } return -1; } //operations void plusFunction(const int n = V.size()) { ret.clear(); vector <string>& a = V[ n-1 ]; vector <string>& b = V[ n-2 ]; for (vector <string> :: iterator k = a.begin(); k != a.end(); k++) ret.push_back( *k ); for (vector <string> :: iterator k = b.begin(); k != b.end(); k++) ret.push_back( *k ); } void multFunction(const int n = V.size()) { ret.clear( ); vector <string>& a = V[ n-1 ]; vector <string>& b = V[ n-2 ]; for (vector <string> :: iterator i = a.begin(); i != a.end(); i++) for (vector <string> :: iterator j = b.begin(); j != b.end(); j++) ret.push_back( *i + *j ); } void checkOperation( char c ) { if ( c == '+' ) plusFunction( ); if ( c == '*' ) multFunction( ); V.erase( V.end() - 2, V.end() ); V.push_back( ret ); } //operations void insert(char c) { if (c == '(' || prio( c ) == -1) { if ( c == '(' ) S.push( c ); else { V.push_back( vector<string>() ); V[ V.size() - 1 ].push_back( c2s( c ) ); } return; } while ( !S.empty() && prio( c ) < prio( S.top() ) ) { if ( S.top() != '(' ) { checkOperation( S.top() ); S.pop(); } else { S.pop(); return; } } S.push( c ); } bool cmp( string a, string b ) { int size_a = a.size(); int size_b = b.size(); if ( size_a != size_b ) return size_a > size_b; return a < b; } int main() { for (cin >> test; test; test--) { cin >> line; line = '(' + line; line = line + ')'; V.clear(); for (string :: iterator c = line.begin(); c != line.end(); c++) insert( *c ); ret = V[ 0 ]; for (vector<string> :: iterator k = ret.begin(); k != ret.end(); k++) sort( k->begin(), k->end() ); sort( ret.begin(), ret.end(), cmp ); bool first_1 = 0; bool first_2 = 0; for (vector<string> :: iterator k = ret.begin(); k != ret.end(); k++) { cout << ( first_1 ? "+" : "" ); for (string :: iterator c = k->begin(); c != k->end(); c++) { cout << ( first_2 ? "*" : "" ) << *c; first_2 = 1; } first_1 = 1; first_2 = 0; } cout << endl; } return 0; }
Judgement Details
Case#1 [421888 bytes][48 ms]: ok 14 tokens