当前位置:网站首页>Solve the problem of adding the least number of parentheses (interval DP)
Solve the problem of adding the least number of parentheses (interval DP)
2022-07-28 20:14:00 【Tiredd】
The question :
The sequence of parentheses consists of (),[],{}, form , for example (([{}]))() It's legal. , and (}{) and (}(} It's all illegal . If a sequence is illegal , Write a program , Add the minimum number of parentheses , Make this sequence legal . for example (}(} Add at least 4 Brackets become legal , Become (){}(){}.
Ideas : Classical interval dp problem , Definition f[l][r] For will c[l] to c[r] The minimum number of parentheses that become legal .
Then when c[l] == c[r] when ,f[l][r] = f[l + 1][r - 1]
When c[l] != c[r] when , f[l][r] = min(f[l + 1][r], f[l][r - 1]) + 1;
Consider again l~r Decision making divided into two intervals f[l][r] = min(f[l][r], f[l][k] + f[k + 1][r])
See code for details :
#include <iostream>
#include <cstring>
using namespace std;
const int N = 110;
char c[N];
int n, f[N][N];
bool cheak(char a, char b)
{
if(a == '[' && b == ']') return true;
if(a == '(' && b == ')') return true;
if(a == '{' && b == '}') return true;
return false;
}
int main()
{
scanf("%s", c);
n = strlen(c);
for(int len = 1; len <= n; len ++ )
for(int l = 0; l + len - 1 < n; l ++ )
{
int r = l + len - 1;
if(len == 1) f[l][r] = 1; // initialization
else
{
f[l][r] = 0x3f3f3f3f;
if(cheak(c[l], c[r])) f[l][r] = f[l + 1][r - 1];
// else f[l][r] = min(f[l + 1][r] + 1, f[l][r - 1] + 1); This decision is contained in the following circular enumeration , So you can comment out
for(int k = l; k < r; k ++ )
f[l][r] = min(f[l][r], f[l][k] + f[k + 1][r]);
}
}
cout << f[0][n - 1] << endl;
return 0;
}边栏推荐
- Advanced notes (Part 2)
- Why is there no log output in the telnet login interface?
- Basic knowledge of C language
- Using Lex (Flex) to generate lexical analyzer of PL language
- Andorid system layout, values, drawable adaptation
- Const pointer of C language and parameter passing of main function
- NEIL: Extracting Visual Knowledge from Web Data
- [C language] scanf format input and modifier summary
- Basic usage of docker
- 8. Compilation errors of C language and Chinese explanation
猜你喜欢

Getting started with enterprise distributed crawler framework

WFST decoding process

JVM (24) -- performance monitoring and tuning (5) -- Analyzing GC logs
![[C language] random number generation and `include < time. H > 'learning](/img/bb/3e47bf2e3b25653d9048884d65cda3.png)
[C language] random number generation and `include < time. H > 'learning
![[NPP installation plug-in]](/img/6f/97e53116ec4ebc6a6338d125ddad8b.png)
[NPP installation plug-in]

“中国网事·感动2022”二季度网络感动人物评选结果揭晓

Reverse string

2. Floating point number, the difference between float and double in C language and how to choose them
![[C language] simulation implementation of strlen (recursive and non recursive)](/img/73/e92fe714515491f1ea366d6924c9ec.png)
[C language] simulation implementation of strlen (recursive and non recursive)

Read how to deploy highly available k3s with external database
随机推荐
Sprint for golden nine and silver ten, stay up at night for half a month, collect 1600 real interview questions from Android post of major manufacturers
What is the process of swing event processing?
Two methods to judge the size end
通信网络基础知识01
1. C language variable type, global variable, local variable
[C language] summary of methods for solving the greatest common divisor
C language pointer and two-dimensional array
Basic mathematical knowledge (update)
flask_ Mail source code error
Kubeedge releases white paper on cloud native edge computing threat model and security protection technology
爬取IP
2. Floating point number, the difference between float and double in C language and how to choose them
Basic knowledge of C language
Prometheus deployment
C language implementation of strncpy
MySQL命令语句(个人总结)
Stories of Party members | Li qingai uses cartoons to drive farmers to increase income and become rich
C language - question brushing column
Saltstack advanced
私有化部署的即时通讯平台,为企业移动业务安全保驾护航