Title Description
Parenthesis give you a valid sequence s1, every time you can remove a "()"
you can remove zero or more "()"
seek sequence can be deleted to another bracket s2
you can remove zero or more "()"
seek sequence can be deleted to another bracket s2
Enter a description:
A first line of input string s (2 ≤ | s | ≤ 100)
a second input line of a character string t (2 ≤ | t | ≤ 100)
Output Description:
If you can output "Possible"
Otherwise, output "Impossible"
Example 1
Entry
(()) ()
Export
Possible
Example 2
Entry
() ()
Export
Possible
Example 3
Entry
(()()()) ((()))
Export
Impossible
Example 4
Entry
((())((())())()) (()(())())
Export
Possible
Example 5
Entry
((())((())())()) ((()()()()()))
Export
Impossible
ideas: the input string is a, the target string b. Deleted when the brackets must always ensure that the number of left bracket and right bracket than or equal to (the last to be equal),
we can define dp [i] [j] [ k] represents consideration before a string of i before the matching string b a j, A character string is deleted from the left bracket portion - Right parenthesis = k number of feasibility, the classification can transfer discussion,
the final answer is dp [n] [m] [ 0]
Code:
1 #include <cstdio> 2 #include <fstream> 3 #include <algorithm> 4 #include <cmath> 5 #include <deque> 6 #include <vector> 7 #include <queue> 8 #include <string> 9 #include <cstring> 10 #include <map> 11 #include <stack> 12 #include <set> 13 #include <sstream> 14 #include <iostream> 15 #define mod 1000000007 16 #define eps 1e-6 17 #define ll long long 18 #define INF 0x3f3f3f3f 19 using namespace std; 20 21 char a[110],b[110]; 22 bool dp[110][110][110]; 23 int main() 24 { 25 scanf("%s%s",(a+1),(b+1)); 26 int m=strlen(a+1),n=strlen(b+1); 27 dp[0][0][0]=1; 28 for(int i=0;i<m;i++) 29 { 30 for(int j=0;j<=n;j++) 31 { 32 for(int k=0;k<=m;k++) 33 { 34 if(dp[i][j][k]) 35 { 36 if(!k&&a[i+1]==b[j+1]) 37 { 38 dp[i+1][j+1][k]=1; 39 } 40 if(a[i+1]=='(') 41 { 42 dp[i+1][j][k+1]=1; 43 } 44 else if(k) 45 { 46 dp[i+1][j][k-1]=1; 47 } 48 } 49 } 50 } 51 } 52 if(dp[m][n][0]) 53 { 54 printf("Possible\n"); 55 } 56 else 57 { 58 printf("Impossible\n"); 59 } 60 }