Codeforces 918C - The Monster (贪心 + 思维 + 栈模拟)
2018-01-31 13:12
387 查看
思路:
首先明确题意:找符合要求的区间个数
很容易想到利用栈的括号匹配,枚举区间进行判断,此题多了问号的判断。
括号匹配不用栈的实现
首先明确题意:找符合要求的区间个数
很容易想到利用栈的括号匹配,枚举区间进行判断,此题多了问号的判断。
括号匹配不用栈的实现
bool check(string s) { int top = 0; for(int i = 0; i < s.size(); ++i) { if(s[i] == '(') top++; else top--; if(cnt < 0) return false; } return cnt == 0; }由此改进出此题的解法
#include <iostream> #include <string> using namespace std; int main() { string s; cin >> s; int ans = 0; for(int i = 0; i < s.size(); ++i) { int low = 0, high = 0; //top's range for(int j = i; j < s.size(); ++j) { if(s[j] == '(') low++, high++; else if(s[j] == ')') low--, high--; else low--, high++; if(high < 0) break; if(low < 0) low = 0; //注意top小于0时需要把问号匹配 if(((j - i) & 1) && low == 0) { ans++; } } } cout << ans << endl; return 0; }
相关文章推荐
- Codeforces 807E Prairie Partition 贪心思维+二分
- CodeForces - 734C Anton and Making Potions 二分+贪心 思维题
- codeforces 232A Cycles 图论 贪心 思维
- Codeforces 500C:New Year Book Reading(思维+贪心?)
- CodeForces - 918C The Monster (暴力)
- codeforces-337【C思维,贪心】
- codeforces-508C-Anya and Ghosts【贪心】【思维】
- Codeforces 478C Table Decorations 贪心(思维!!!)
- Codeforces 798D Mike and distribution【思维+贪心】好题!好题!感觉智商受到了一万点伤害= =
- codeforces 918C(思维)
- CodeForces - 839B Game of the Rows 贪心+思维
- Codeforces 478C Table Decorations【贪心】【思维】
- Codeforces - 478C. Table Decorations - 思维、贪心
- Codeforces 349 B Color the Fence (思维+贪心)
- Codeforces 798C Mike and gcd problem【思维+贪心】好题!
- codeforces 913 C. Party Lemonade【思维贪心】
- CodeForces 798D Mike and distribution(二维贪心 + 思维)
- Codeforces 526C - Om Nom and Candies (贪心 + 思维)
- CodeForces - 798C Mike and gcd problem (贪心+思维)
- CodeForces 500C New Year Book Reading(贪心+思维)