标签:
4 [] ([])[] ((] ([)]
0 0 3 2题解:类似与区间dp;
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 string str; 5 int dp[111][111]; 6 int main(){ 7 int N; 8 cin>>N; 9 while(N--){ 10 str.clear(); 11 cin>>str; 12 for(int i=0;i<str.size();i++)dp[i][i]=1;//又是多了个等号,无语== 13 for(int i=1;i<str.size();i++){ 14 for(int j=i-1;j>=0;j--){ 15 dp[j][i]=dp[j][i-1]+1; 16 for(int k=j;k<i;k++){ 17 if(str[k]-str[i]==-1||str[k]-str[i]==-2) 18 dp[j][i]=min(dp[j][i],dp[j][k-1]+dp[k+1][i-1]); 19 //cout<<dp[j][i]<<" "; 20 } 21 } 22 } 23 cout<<dp[0][str.size()-1]<<endl; 24 } 25 return 0;}
刚开始用栈写的,没考虑清楚,果断错了;
代码:例如【))】
1 #include<stdio.h> 2 #include<stack> 3 #include<map> 4 using namespace std; 5 char s[110]; 6 int main(){ 7 int N,tot; 8 scanf("%d",&N); 9 while(N--){map<char,int>mp; 10 stack<char>sign; 11 tot=0; 12 mp[‘(‘]=1;mp[‘)‘]=-1; 13 mp[‘[‘]=2;mp[‘]‘]=-2; 14 scanf("%s",s); 15 for(int i=0;s[i];i++){ 16 if(mp[s[i]]>0)sign.push(s[i]); 17 else{ 18 if(!sign.empty()){ 19 if(mp[sign.top()]+mp[s[i]]==0)sign.pop(); 20 else tot++; 21 } 22 else tot++; 23 } 24 } 25 while(!sign.empty()){ 26 sign.pop(); 27 tot++; 28 } 29 printf("%d\n",tot); 30 } 31 return 0;}
标签:
原文地址:http://www.cnblogs.com/handsomecui/p/4719004.html