题目1342:寻找最长合法括号序列II---注意:不要求就近匹配,只要求( 在 )前面的任一个位置即可
#include<stdio.h> int main() { char str[1000000]; while(scanf("%s",str)!=EOF) { int temp=0,ans=0; for(int i = 0; str[i]; i++) { if(str[i] == '(') { temp++; } else if(temp > 0) { temp--;//保证)前面有匹配的(的存在 ans += 2; } } printf("%d\n",ans); } return 0; }

浙公网安备 33010602011771号