开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第29天,点击查看活动详情
D - Scope Editorial /
Time Limit: 2 sec / Memory Limit: 1024 MB
Score : 400400 points
Problem Statement
A string consisting of lowercase English letters, (, and ) is said to be a good string if you can make it an empty string by the following procedure:
- First, remove all lowercase English letters.
- Then, repeatedly remove consecutive
()while possible.
For example, ((a)ba) is a good string, because removing all lowercase English letters yields (()), from which we can remove consecutive () at the 22-nd and 33-rd characters to obtain (), which in turn ends up in an empty string.
You are given a good string SS. We denote by S_iSi the ii-th character of SS.
For each lowercase English letter a, b, \ldots…, and z, we have a ball with the letter written on it. Additionally, we have an empty box.
For each i = 1,2,i=1,2, \ldots… ,|S|,∣S∣ in this order, Takahashi performs the following operation unless he faints.
- If
S_iSiis a lowercase English letter, put the ball with the letter written on it into the box. If the ball is already in the box, he faints. - If
S_iSiis(, do nothing. - If
S_iSiis), take the maximum integerjjless thaniisuch that thejj-th throughii-th characters ofSSform a good string. (We can prove that such an integerjjalways exists.) Take out from the box all the balls that he has put in thejj-th throughii-th operations.
Determine if Takahashi can complete the sequence of operations without fainting.
Constraints
1 \leq |S| \leq 3 \times 10^51≤∣S∣≤3×105SSis a good string.
Input
The input is given from Standard Input in the following format:
SS
Output
Print Yes if he can complete the sequence of operations without fainting; print No otherwise.
Sample Input 1 Copy
Copy
((a)ba)
Sample Output 1 Copy
Copy
Yes
For i = 1i=1, he does nothing.
For i = 2i=2, he does nothing.
For i = 3i=3, he puts the ball with a written on it into the box.
For i = 4i=4, j=2j=2 is the maximum integer less than 44 such that the jj-th through 44-th characters of SS form a good string, so he takes out the ball with a written on it from the box.
For i = 5i=5, he puts the ball with b written on it into the box.
For i = 6i=6, he puts the ball with a written on it into the box.
For i = 7i=7, j=1j=1 is the maximum integer less than 77 such that the jj-th through 77-th characters of SS form a good string, so he takes out the ball with a written on it, and another with b, from the box.
Therefore, the answer to this case is Yes.
Sample Input 2 Copy
Copy
(a(ba))
Sample Output 2 Copy
Copy
No
For i = 1i=1, he does nothing.
For i = 2i=2, he puts the ball with a written on it into the box.
For i = 3i=3, he does nothing.
For i = 4i=4, he puts the ball with b written on it into the box.
For i = 5i=5, the ball with a written on it is already in the box, so he faints, aborting the sequence of operations.
Therefore, the answer to this case is No.
Sample Input 3 Copy
Copy
(((())))
Sample Output 3 Copy
Copy
Yes
Sample Input 4 Copy
Copy
abca
Sample Output 4 Copy
Copy
No
分析
这题就算用栈模拟一下,然后如果在消除前不能让所有的字母的个数都小于等于1,就yes了。
代码
#include <iostream>
#include <algorithm>
#define ll long long
#include <cmath>
#include <stack>
#include <cstring>
using namespace std;
const int N=300010;
int a[N];
stack<char>stk;
int main(){
string s;
cin>>s;
int cnt[30];
memset(cnt,0,sizeof cnt);
for(int i=0;i<s.size();i++){
if(s[i]=='('){
stk.push(s[i]);
}
else if(s[i]>='a' && s[i]<='z'){
int now=s[i]-'a';
//cout<<cnt[now]<<endl;
if(cnt[now]==1){
puts("No");
return 0;
}
cnt[now]++;
stk.push(s[i]);
}
else{
while(stk.top()!='(' && !stk.empty()){
cnt[stk.top()-'a']--;
stk.pop();
}
if(!stk.empty())
stk.pop();
}
//cout<<"****************************************8"<<endl;
}
puts("Yes");
}