Numbers in parentheses beef (DP)

Links: https://ac.nowcoder.com/acm/problem/21652

Source: Cattle-off network

Title Description

Taurus recently started to learn pull opening parenthesis matching
Give you two brackets sequences, does not guarantee the legitimate, ask how many different ways can be combined into a sequence of two brackets legitimate parenthesis sequence
Time of the merger can not change their original order sequence

Enter a description:

Comprises two lines of two strings S1, S2 

. 1 ≤ | S1 |, | S2 | ≤ 2500

Output Description:

Output an integer, mod 1e9 + 7

Specific ideas:

DP [i] [j] denotes the i-th and j th number in '(' ratio '') before the multi-premise before the first character string a second embodiment; this process requires an additional opening array used this recording process.

And then a burst of memory; fight like a rolling array.

MLE Code:

 1 #pragma comment(linker, "/STACK:102400000,102400000")
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 # define ll long long
 5 # define inf 0x3f3f3f3f
 6 # define ll_inf (1ll<<60)
 7 # define lson l,mid,rt<<1
 8 # define rson mid+1,r,rt<<1|1
 9 const int maxn = 2500+10;
10 const int  N = 15;
11 const int mod = 1e9+7;
12 ll dp[maxn][maxn];
13 int pre[maxn][maxn];
14 char str1[maxn],str2[maxn];
15 int main()
16 {
17     scanf("%s",str1+1);
18     scanf("%s",str2+1);
19     int len1=strlen(str1+1);
20     int len2=strlen(str2+1);
21     dp[0][0]=1;
22     for(int i=0; i<=len1; i++)
23     {
24         for(int j=0; j<=len2; j++)
25         {
26             if(!i&&!j)continue;
27             if(!j)pre[i][j]=pre[i-1][j]+(str1[i]=='('?1:-1);
28             else pre[i][j]=pre[i][j-1]+(str2[j]=='( ' ? . 1 : - . 1 );
 29              IF (pre [i] [j] < 0 ) // if the first character string before the i-th and j-th second string before scrape out is smaller than 0, the illegal
 30                  Continue ;
 31 is              IF (I)
 32                  DP [I] [J] + DP = [I- . 1 ] [J];
 33 is              IF (J)
 34 is                  DP [I] [J] + DP = [I] [J- . 1 ];
 35                  DP [I] [J]% = MOD;
 36          }
 37 [      }
 38 is      the printf ( " % LLD \ n- " , pre [LEN1] [LEN2] == 0 DP [LEN1] [LEN2]?:0);
39     return 0;
40 }

AC Code:

 1 #pragma comment(linker, "/STACK:102400000,102400000")
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 # define ll long long
 5 # define inf 0x3f3f3f3f
 6 # define ll_inf (1ll<<60)
 7 # define lson l,mid,rt<<1
 8 # define rson mid+1,r,rt<<1|1
 9 const int maxn = 2500+10;
10 const int  N = 15;
11 const int mod = 1e9+7;
12 int dp[3][maxn];
13 int pre[3][maxn];
14 char str1[maxn],str2[maxn];
15 int main()
16 {
17     scanf("%s",str1+1);
18     scanf("%s",str2+1);
19     int len1=strlen(str1+1);
20     int len2=strlen(str2+1);
21     int cur=0;
22     dp[cur][0]=1;
23     for(int i=0; i<=len1; i++)
24     {
25         if(i)
26         {
27             cur^=1;
28             memset(dp[cur],0,sizeof(dp[cur]));
29         }
30         // pre[i][0]=
31         for(int j=0; j<=len2; j++)
32         {
33             if(!i&&!j)
34                 continue;
35             if(!j)
36                 pre[cur][j]=pre[cur^1][j]+(str1[i]=='('?1:-1);
37             else
38                 pre[cur][j]=pre[cur][j-1]+(str2[j]=='('?1:-1);
39             if(pre[cur][j]<0)
40                 continue;
41             if(i)
42                 dp[cur][j]+=dp[cur^1][j];
43             if(j)
44                 dp[cur][j]+=dp[cur][j-1];
45             dp[cur][j]%=mod;
46         }
47     }
48     printf("%d\n",pre[cur][len2]==0?dp[cur][len2]:0);
49     return 0;
50 }

Learning Website: https://blog.csdn.net/j2_o2/article/details/87971817#commentBox

Guess you like

Origin www.cnblogs.com/letlifestop/p/10974541.html