蒟蒻来讲题,还望大家喜。若哪有问题,大家尽可提!
Hello, 大家好哇!本蒟蒻今天来讲一下AtCoder Beginner Contest 283的D题——Scope
=============================================================================
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 2-nd and 3-rd characters to obtain ()
, which in turn ends up in an empty string.
You are given a good string S. We denote by Si the i-th character of S.
For each lowercase English letter a
, b
, …, 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, … ,|S|,in this order, Takahashi performs the following operation unless he faints.
- If Si is 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 Si is
(
, do nothing. - If Si is
)
, take the maximum integer j less than i such that the j-th through i-th characters of S form a good string. (We can prove that such an integer j always exists.) Take out from the box all the balls that he has put in the j-th through i-th operations.
Determine if Takahashi can complete the sequence of operations without fainting.
Constraints
- 1 ≤ |S| ≤ 3 × 10^5
- S is a good string.
Input
The input is given from Standard Input in the following format:
S
Output
Print Yes
if he can complete the sequence of operations without fainting; print No
otherwise.
Sample Input 1
((a)ba)
Sample Output 1
Yes
For i = 1, he does nothing.
For i = 2, he does nothing.
For i = 3, he puts the ball with a
written on it into the box.
For i = 4,j=2 is the maximum integer less than 4 such that the j-th through 4-th characters of S form a good string, so he takes out the ball with a
written on it from the box.
For i = 5, he puts the ball with b
written on it into the box.
For i = 6, he puts the ball with a
written on it into the box.
For i = 7, j=1 is the maximum integer less than 7 such that the j-th through 7-th characters of S 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
.
主要意思
给出一个字符串,一定是一个好的字符串——好的字符串:把字符串中的字母删掉,剩下的括号正好匹配。之后,让你遇到字母写在球,并把球放在盒子里,若已经有一个相同的字母在盒子里,那么输出“No";遇到右括号,找到最近的匹配的左括号,且坐标最大,并把两个括号之间所有的字母都从盒子里拿出来。最后,若不输出"No"输出”Yes"。
思路
我们可以用一个vector存储一下左括号的位置,每遇到一个右括号则最近的左括号(也就是vector中存储的最后一个元素)就是最大的匹配的一个左括号。最后,用一个数组存储每个元素的最后一个位置,然后枚举26个英文字母(不能枚举区间!!!),看看哪个字母的最后一个位置在区间之间。
之后分情况
①若该字符为字母,则判断是否已有,若有输出"No"结束程序;否则标记已有,并更新当前字母的最后一个位置。
②若该字符为(,那么放入vector。
③若该字符为),那么判断并排出左括号。
最后,如果没有输出No,那么输出Yes。
代码
#include <iostream>
#include <vector>
#include <map>
using namespace std;
const int N = 3e5 + 10;
string s;
vector<int> last_kuo;
map<char, bool> check;
int tt[27];
int j;
int main()
{
cin >> s;
for (int i = 0; i < s.size(); i ++)
{
//字符
if (isalpha(s[i]))
if (check[s[i]])//判断是否已有
{
cout << "No" << endl;
return 0;
}
else
check[s[i]] = 1, tt[s[i]-'a'] = i;//没有标记为1,并更新最后一个字符的位置
//左括号
else if (s[i] == '(')
last_kuo.push_back(i);//将左括号放入vector
//右括号
else
{
j = last_kuo.back();//最后一个左括号位置之
for(int x = 0; x < 26; x ++)
if(tt[x] > j) check['a' + x] = 0; //判断每个字符的最后一个位置是否在区间内
last_kuo.pop_back();//将此左括号排除
}
}
cout << "Yes" << endl;
return 0;
}
好了,今天就到这里啦!
本初中生蒟蒻时间紧迫,临近期末写一篇不容易——所以,可能写的有点粗略,若有哪不懂,尽管问,并且麻烦喜欢的点个赞,关个注,谢谢!~