洛谷 P13202 [GCJ 2016 #3] Teaching Assistant 题解
解题思路
我们令你申请题集的类型为 ,此时助教的心情为 ,你提交题集的类型为 ,此时助教的心情为 ,助教的打分为 。
那么有如下情况:
很容易发现这是贪心算法,那么我们可以这样:
- 当遇到与栈顶字符相同的字符时,弹出栈顶并计数一次相同配对。
- 当遇到与栈顶字符不同的字符时,将当前字符压入栈中。
答案
令课程持续 天,相同配对有 个,则有如下方程。
时间复杂度
,其中 指字符串 的长度(过不了我道理蜡西)。
AC Code
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin >> T;
while (T--) {
string s;
cin >> s;
int n = s.size() >> 1;
vector<char> st;
int cnt = 0;
for (char c : s) {
if (!st.empty() && st.back() == c) {
st.pop_back();
cnt++;
} else st.push_back(c);
}
int ans = 5 * (n + cnt);
cout << "Case #" << t << ": " << ans << '\n';
}
return 0;
}完结撒花!