Stay Hungry,Stay Foolish!

D - Scope --- ATCODER

D - Scope

https://atcoder.jp/contests/abc283/tasks/abc283_d

 

思路

使用stack做字符串的内容分析, 除了)所有的字符依次入栈, 遇到(字符,则从栈顶开始依次出栈,直到第一个(也被出栈;

使用set做已入栈的小写字母统计, 在小写字母入栈过程,如果在set中已经存在,则报错。

 

Code

https://atcoder.jp/contests/abc283/submissions/37616156

string s;
stack<char> cstack;
set<char> cset;
 
int main()
{
    cin >> s;
 
    int size = s.size();
    for(int i=0; i<size; i++){
        char one = s[i];
        
        if(one >= 'a' && one <= 'z'){
            if (cset.count(one) >= 1){
                cout << "No" << endl;
                return 0;
            }
            
            cset.insert(one);
            cstack.push(one);
        } else if (one == '('){
            cstack.push(one);
        } else if (one == ')'){
            while(!cstack.empty()){
                char onetop = cstack.top();
                cstack.pop();
                if (onetop == '('){
                    break;
                } else {
                    cset.erase(onetop);
                }
            }
        }
    }
 
    cout << "Yes" << endl;
 
    return 0;
}

 

posted @ 2023-01-01 22:53  lightsong  阅读(48)  评论(0编辑  收藏  举报
Life Is Short, We Need Ship To Travel