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; }
出处:http://www.cnblogs.com/lightsong/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。