QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#217611 | #5108. Prehistoric Programs | drldrls | RE | 0ms | 0kb | C++17 | 4.3kb | 2023-10-17 02:34:35 | 2023-10-17 02:34:35 |
answer
#include<bits/stdc++.h>
using namespace std;
bool compare_neg_score(const vector<int>& a, const vector<int>& b){
return a[2] >= b[2];
}
int main(){
// To get input n
int count{0};
cin >> count;
// Process of solving this problem:
// 1. First, in order to make a properly nested structure, the counts of '(' and ')' has to be the same.
// Let the total '(' - ')' be total_score, and each string also has its score.
// 2. In the final sequence, we will always have all strings with positive scores in the front,
// and those with negative scores in the back, so there wont be strings like "))((" happens.
// 3. But in positive strings, there could be a substring of negative scores. E.g. ")))((((((".
// Despite it has positive score, it cannot be paired with some positive strings like "(" in the front.
// Thus, we need to calculate another score "neg_score" to indicate the substring (from the start of the string) with smallest negative score.
// 4. If all positive strings has neg_score < 0, then it's "impossible", since it must starts with ')'.
// 5. Sort all positive strings by their neg_score (decreasing).
// This can guaruntee we find the possible answer without choose something like "))((" as the first string and return "impossible".
// Check each time if the total_score - new neg_score is >= 0. If not, return "impossible".
// 6. After checking all the positive strings, check the negative strings for the sequence.
// Reverse all negative string and check like positive strings. It's like checking the string from behind.
// Reverse means flip all ( & ) and reverse the order.
// When they meet in the middle, we can check if total_score = 0.
// 7. If it's a valid nested structure (total_score = 0), print out the sequence. Else print "impossible".
// *strings with score = 0 are stored to positive strings.
vector<vector<int>> pos_strings(0); // save strings with positive score
vector<vector<int>> neg_strings(0); // save strings with negative score
bool wrong = false; // save whether it's impossible
// Getting input strings
for(int i = 0; i < count; i++){
string s;
int score{0}, neg_score{0}, temp_neg{0}, pos_score{0}, temp_pos{0};
cin >> s;
// calculate score & neg_score. Fkip the negative strings
score = 2 * std::count(s.begin(), s.end(), '(') - s.length();
if(score < 0){
for(char c: s){
if(c == '(') c = ')';
else c = '(';
}
reverse(s.begin(), s.end());
}
for(char c: s){
if(c == '('){
temp_neg++;
score++;
}else{
temp_neg--;
score--;
}
neg_score = temp_neg < neg_score ? temp_neg : neg_score;
}
// save the strings to corresponding vectors
vector<int> v = {i + 1, score, neg_score};
if(score >= 0) pos_strings.emplace_back(v);
else neg_strings.emplace_back(v);
}
// sort positive strings by neg_score (function defined above)
sort(pos_strings.begin(), pos_strings.end(), compare_neg_score);
sort(neg_strings.begin(), neg_strings.end(), compare_neg_score);
// check each round that if the score went below 0. If so, print "impossible" and return
int total_pos_score{0}; // save the current score
for(auto s: pos_strings){
total_pos_score += s[1];
if(total_pos_score + s[2] < 0){
cout << "impossible";
return 0;
}
}
// check negatve strings
int total_neg_score{0}; // save the current score
for(auto s: neg_strings){
total_neg_score -= s[1];
if(total_neg_score + s[2] < 0){
cout << "impossible";
return 0;
}
}
if(total_pos_score != total_neg_score) cout << "impossible";
else{
// print results after passing all the checks, print neg strings in reverse order
for(auto s: pos_strings) cout << s[0] << " ";
for(auto it = neg_strings.rbegin(); it != neg_strings.rend(); it++) cout << (*it)[0] << " ";
}
return 0;
}
详细
Test #1:
score: 0
Runtime Error
input:
50000 ( ( ))))()))()(()))()()()))()(((((()(((()))()(((()))((()(())))))(()( ) ( ) ((( ( ( ( ( ( () ( ) ( )((())()(( )))))( ( ) )) )() ( ) ) )()( ( ( () ( ) ( )()((((())()))())( ( ( )( ( ( (()())()) ) ) ( ( ( )((())))((())))))))))((((()()))()))))))))((()())())) ) )() ) ) ) ) ) ())(())))))()(()((()(())...