当前位置:网站首页>Longest bracket match (linear DP)
Longest bracket match (linear DP)
2022-07-19 06:06:00 【lovesickman】
The longest bracket matches
Title Description
For one by (,),[,] A string of parentheses , Find the longest bracket matching substring . say concretely , A string that meets the following conditions becomes a string that matches parentheses :
1.(),[] Is a string matching parentheses .
2. if A Is a string matching parentheses , be (A),[A] Is a string matching parentheses .
3. if A,B Is a string matching parentheses , be AB It is also a string matching parentheses .
for example :(),[],([]),()() Are strings that match parentheses , and ][,[(]) It is not .
character string A The substring of is defined by A A string of consecutive characters in .
for example ,A,B,C,ABC,CAB,ABCABCd All are ABCABC The string of . An empty string is a substring of any string .
Input format
The input line , Is a ()[] A non empty string composed of .
Output format
There is only one line of output , Match substrings for the longest parentheses . If there are substrings of the same length , The substring with the front output position .
Examples #1
The sample input #1
([(][()]]()
Sample output #1
[()]
Examples #2
The sample input #2
())[]
Sample output #2
()
Tips
【 Data range 】
Yes 20% The data of , String length <=100.
Yes 50% The data of , String length <=10000.
Yes 100% The data of , String length <=1000000.
reputation , Think of a stack simulation , whole WA.
Not considered
([]())
vector<char>tmp,res;
stack<char>stk,empty;
void solve(){
string s;
cin>>s;
for(auto c:s){
// debug(stk.size());
if(c == '[' || c == '(' ){
stk.push(c);
}
else{
if(!stk.size()){
tmp.clear();
continue;
}
auto top = stk.top();
if((top == '(' && c == ']') || (top == '[' && c==')')){
stk = empty;
tmp.clear();
}
else{
// top and c Match the
stk.pop();
tmp.pb(c);
if(res.size()<tmp.size()){
res = tmp;
}
}
}
}
reverse(all(res));
for(auto c:res){
if(c == ']')
cout<<'[';
else
cout<<'(';
}
reverse(all(res));
for(auto c:res){
cout<<c;
}
}
A very clever question .
Maybe I forget to do it next time
consider f [ i − 1 ] f[i-1] f[i−1] The meaning of
/* A: 10min B: 20min C: 30min D: 40min */
#include <iostream>
#include <stack>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <sstream>
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define mem(f, x) memset(f,x,sizeof(f))
#define fo(i,a,n) for(int i=(a);i<=(n);++i)
#define fo_(i,a,n) for(int i=(a);i<(n);++i)
#define debug(x) cout<<#x<<":"<<x<<endl;
#define endl '\n'
using namespace std;
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
template<typename T>
ostream& operator<<(ostream& os,const vector<T>&v){
for(int i=0,j=0;i<v.size();i++,j++)if(j>=5){
j=0;puts("");}else os<<v[i]<<" ";return os;}
template<typename T>
ostream& operator<<(ostream& os,const set<T>&v){
for(auto c:v)os<<c<<" ";return os;}
template<typename T1,typename T2>
ostream& operator<<(ostream& os,const map<T1,T2>&v){
for(auto c:v)os<<c.first<<" "<<c.second<<endl;return os;}
template<typename T>inline void rd(T &a) {
char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {
if (c == '-')f = -1; c = getchar();}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}
typedef pair<int,int>PII;
typedef pair<long,long>PLL;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e6+10;
int n,m,_;
int f[N];
void solve(){
string s;
cin>>s;
n = s.size();
s = " " + s;
int ans = 0;
for(int i=1;i<=n;i++){
char c = s[i];
if(c == ']' || c == ')' ){
int l = i - f[i-1] - 1;
if((c == ']' && s[l] == '[') || (c == ')' && s[l] == '(')){
f[i] = f[i-f[i-1]] + 2;
}
}
ans = max(ans,f[i]);
}
cout<<ans<<endl;
}
int main(){
solve();
return 0;
}
边栏推荐
- Simple chrome script automatically skips the charging acknowledgment page after the video playback of station B ends
- golang高并发特性goroutine介绍
- Ehab the Xorcist (异或性质,构造)
- js变量提升
- Go language introduction and application scenario analysis
- Minio installation, deployment and simple use
- 谷歌浏览器不能手动修改cookies,cookie报红标红
- Golang multi project workspace construction
- 嵌入式C语言重点(const、static、voliatile、位运算)
- 【力扣】相同的树
猜你喜欢

RestClient-多条件聚合

[antdv: Each record in table should have a unique `key` prop,or set `rowKey` to an unique.....

VSCode 即时英文翻译插件 【翻译(英汉词典)】

2021-11-26 pyautogui cooperates with lightning simulator to realize the automation of mobile app check-in and answer questions

Minio installation, deployment and simple use

Golang multi project workspace construction

自动补全 & (自定义)拼音分词器 & 搜索时注意事项

Loadng class `com.mysql.jdbc.Driver‘. This is deprecated. The new driver class is `com.mysql.cj.jdb

MySQL Workbench基本使用【创建一个数据库】

Cours de mathématiques de base 2 Fonction Euler, écran linéaire, élargissement de l'Europe
随机推荐
计算几何(2)
[antdv: Each record in table should have a unique `key` prop,or set `rowKey` to an unique.....
Material and application circuit diagram of 0-10V, 4-20mA current voltage to PWM isolation converter
2022 RoboCom 世界机器人开发者大赛-本科组(省赛)
DSL实现自动补全查询
5V boost charging 8.4v chip
Ehab the Xorcist (异或性质,构造)
设置索引库结构,给用户添加可自动补全的suggestion,并将一些字段变成集合放到suggestion里面去
js变量提升
【牛客】二叉树遍历
HRA isolation series wide voltage input positive and negative high voltage regulated output
处理中文分词 ik分词器以及拓展和停止字典
升高压模块隔离模块HRA2460D-2W
RestAPI实现自动补全 & 案例实现(搜索框输入进行自动补全)
Go language introduction and application scenario analysis
【力扣】翻转二叉树
简单Chrome脚本 自动跳过b站视频播放结束后的的充电鸣谢页面
Analog signal in-depth discussion on the transmission distance of 4-20mA current signal
2021-11-17 esp32 pin reference
HM agent multi section lithium battery charging IC