4 条题解
-
0
#include <iostream> #include <algorithm> #include <stack> #include <string> using namespace std; stack<int>stk; string s; int main() { cin>>s; int ans=0; for(int i=0;i<s.size();i++){ if(stk.size()){ char t=s[stk.top()]; if(s[i]=='}'&&t=='{'||s[i]==')'&&t=='('||s[i]==']'&&t=='[') stk.pop(); else stk.push(i); } else stk.push(i); if(stk.size()) ans=max(ans,i-stk.top()); else ans=max(ans,i+1); } cout<<ans<<endl; return 0; }
-
0
C++ :
#include <iostream> #include <algorithm> #include <stack> #include <string> using namespace std; stack<int>stk;//栈中存相应字符的下标 string s; int main() { cin>>s; int ans=0; for(int i=0;i<s.size();i++){ if(stk.size()){ char t=s[stk.top()]; if(s[i]=='}'&&t=='{'||s[i]==')'&&t=='('||s[i]==']'&&t=='[') stk.pop(); else stk.push(i); } else stk.push(i); if(stk.size()) ans=max(ans,i-stk.top()); else ans=max(ans,i+1); } cout<<ans<<endl; return 0; }
- 1
信息
- ID
- 61
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 2
- 标签
- 递交数
- 103
- 已通过
- 61
- 上传者