poj-2955-Brackets-區間DP

poj-2955-Brackets-區間DP

?

Brackets
Time Limit:?1000MS?Memory Limit:?65536K
Total Submissions:?9014?Accepted:?4829

Description

We give the following inductive definition of a “regular brackets” sequence:

  • the empty sequence is a regular brackets sequence,
  • if?s?is a regular brackets sequence, then (s) and [s] are regular brackets sequences, and
  • if?a?and?b?are regular brackets sequences, then?ab?is a regular brackets sequence.
  • no other sequence is a regular brackets sequence

For instance, all of the following character sequences are regular brackets sequences:

(), [], (()), ()[], ()[()]

while the following character sequences are not:

(, ], )(, ([)], ([(]

Given a brackets sequence of characters?a1a2?…?an, your goal is to find the length of the longest regular brackets sequence that is a subsequence of?s. That is, you wish to find the largest?m?such that for indices?i1,?i2, …,?im?where 1 ≤?i1?<?i2?< … <?im?≤?n,?ai1ai2?…?aim?is a regular brackets sequence.

Given the initial sequence?([([]])], the longest regular brackets subsequence is?[([])].

Input

The input test file will contain multiple test cases. Each input test case consists of a single line containing only the characters?(,?),?[, and?]; each input test will have length between 1 and 100, inclusive. The end-of-file is marked by a line containing the word “end” and should not be processed.

Output

For each input case, the program should print the length of the longest possible regular brackets subsequence on a single line.

Sample Input

((()))
()()()
([]])
)[)(
([][][)
end

Sample Output

6
6
4
0
6

Source

Stanford Local 2004

?

?

一開始使用code2,找到總的pair數量再*2,以為就解決了。但是wrong answer,不知道本code的方法錯在哪里?

使用了區間DP,方法參考自:?http://www.cnblogs.com/kuangbin/archive/2013/04/29/3051402.html

?

2955Accepted472K47MSG++858B2017-09-16 11:12:08

?

#include <cstdio> 
#include <cstring>const int MAXN = 110; #define max(a, b) (a)>(b)?(a):(b) char ch[MAXN]; 
int dp[MAXN][MAXN]; int Solve(int l, int r){if(dp[l][r] != -1){return dp[l][r]; }if(l>=r){dp[l][r] = 0; return 0; }else if(r == l +1 ){if((ch[l]=='(' && ch[r]==')') ||  (ch[l]=='[' && ch[r]==']') ){dp[l][r] = 2; }else{dp[l][r] = 0; }return dp[l][r]; }dp[l][r] = Solve(l+1, r); for(int k=l+1; k<=r; ++k){if((ch[l]=='(' && ch[k]==')') || (ch[l]=='[' && ch[k]==']')  ){dp[l][r] = max(dp[l][r], 2+Solve(l+1, k-1) + Solve(k+1, r)); }}return dp[l][r]; 
}int main(){freopen("in.txt", "r", stdin); while(scanf("%s", ch) != EOF){getchar(); if(strcmp(ch, "end") == 0){break; }int len = strlen(ch); memset(dp, -1, sizeof(dp)); int ans = Solve(0, len-1); printf("%d\n", ans );}return 0; 
}

  

?

采用count的方式來計算 subsequence,不知道wrong answer的地方是啥?

?

#include <cstdio>  
#include <cstdlib>  #include <cstring> const int MAXN = 12; int main(){freopen("in.txt", "r", stdin);  int ans, f1,f2, len; char ch[MAXN]; while(scanf("%s", ch) != EOF){getchar(); if(strcmp(ch, "end") == 0){break; } len = strlen(ch); ans = 0;f1 = 0; f2 = 0;  for(int i=0; i<len; ++i){if(ch[i] == '('){++f1; }else if(ch[i] == '['){++f2; }else if(ch[i] == ')'){if(f1 > 0){++ans; --f1; }}else if(ch[i] == ']'){if(f2 > 0){++ans; --f2; }}}printf("%d\n", 2*ans );} return 0; 
} 

  

?

轉載于:https://www.cnblogs.com/zhang-yd/p/7530650.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/371197.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/371197.shtml
英文地址,請注明出處:http://en.pswp.cn/news/371197.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Python調用(運行)外部程序

在Python中可以方便地使用os模塊運行其他的腳本或者程序&#xff0c;這樣就可以在腳本中直接使用其他腳本&#xff0c;或者程序提供的功能&#xff0c;而不必再次編寫實現該功能的代碼。為了更好地控制運行的進程&#xff0c;可以使用win32process模塊中的函數。如果想進一步控…

Java中已檢查和未檢查的異常

Java有兩種類型的異常-已檢查和未檢查。 簡而言之&#xff0c;選中的是指開發人員可以從異常中合理恢復的情況&#xff0c;而未選中的異常是無法處理的編程錯誤。 本文介紹了何時使用哪種。 但這不是那么簡單–受檢查的異常使代碼變得“丑陋”。 它們迫使開發人員編寫try / cat…

CCF - 201403-3 - 命令行選項

問題描述 試題編號&#xff1a;201403-3試題名稱&#xff1a;命令行選項時間限制&#xff1a;1.0s內存限制&#xff1a;256.0MB問題描述&#xff1a; 問題描述請你寫一個命令行分析程序,用以分析給定的命令行里包含哪些選項。每個命令行由若干個字符串組成,它們之間恰好由一個空…

java 枚舉 values_JAVA 枚舉運用一 values方法

importjava.lang.reflect.Method;importjava.lang.reflect.Type;importjava.util.Set;import java.util.*;public classEnumJavaClass {public enumEnumClass{One("參數變量枚舉一"),Two("參數變量枚舉二"),Three("參數變量枚舉三");privateStri…

telnet測試端口是否正常打開

點擊計算機的開始菜單--》運行 &#xff0c;輸入CMD命令&#xff0c;然后確定。打開cmd命令行。 輸入telnet測試端口命令&#xff1a; telnet IP 端口 或者 telnet 域名 端口 回車 如果端口關閉或者無法連接&#xff0c;則顯示不能打開到主機的鏈接&#xff0c;鏈接失敗 端口…

Linux歷史,安裝,分區,版本

Linux 歷史 1970年是 UNIX元年&#xff0c;這一年 Kenneth Lane Thompson 和 Dennis Ritchie 合作編寫了UNIX系統。Stallman 發起了GNU 計劃&#xff0c;他本人開發了Emacs, GCC, GDB.Minix&#xff1a;教學用的類UNIX系統&#xff0c;由于UNIX是收費的且價格昂貴&#xff0c;因…

放棄Eclipse Juno

在上一個博客中&#xff0c;我發布了有關Eclipse 4.2 Juno設置的信息。 萬一我需要重新安裝其他東西&#xff0c;也可以作為參考。 當時我沒有談論的是我與Juno共同遇到的問題。 我以為這是我自己的安裝程序&#xff0c;很麻煩&#xff0c;但是此后并沒有太大改善。 我遇到的主…

Java instead of 用法_我又不是你的誰--java instanceof操作符用法揭秘

背景故事《曾經最美》是朱銘捷演唱的一首歌曲&#xff0c;由陳佳明填詞&#xff0c;葉良俊譜曲&#xff0c;是電視劇《水晶之戀》的主題曲。歌曲時長4分28秒。 歌曲歌詞&#xff1a;看不穿你的眼睛藏有多少悲和喜像冰雪細膩又如此透明仿佛片刻就要老去整個城市的孤寂不止一個你…

3.26

http://codeforces.com/gym/101196/attachments A題 B題 題意&#xff1a;一群人玩桌上足球(>4人)&#xff0c;分成黑白兩隊&#xff0c;每隊有進攻和防守兩名玩家&#xff0c;如果有一方失敗則失敗方的防守坐到等候席的結尾、進攻被流放到防守區再上來一個人作為進攻方。而…

scala akka通信機制

https://www.2cto.com/kf/201701/587514.html轉載于:https://www.cnblogs.com/rocky-AGE-24/p/7542874.html

JUnit通過失敗測試案例

為什么要建立一種預期測試失敗的機制&#xff1f; 有一段時間&#xff0c;人們會希望并期望JUnit Test案例失敗。 盡管這種情況很少見&#xff0c;但確實發生了。 我需要檢測JUnit測試何時失敗&#xff0c;然后&#xff08;如果期望的話&#xff09;通過而不是失敗。 具體情況是…

CentOS6.5安裝MySQL5.7詳細教程

CentOS6.5安裝MySQL5.7詳細教程 注&#xff1a;文中所寫的安裝過程均在CentOS6.5 x86下通過測試 主要參考博文&#xff1a; https://segmentfault.com/a/1190000003049498 http://www.th7.cn/db/mysql/201601/175073.shtml 1.檢測系統是否已經安裝過mysql或其依賴&#xff0c;若…

cmake 查看編譯命令,以及在vscode中如何使用cmke

通過設置如下配置選項&#xff0c;可以生成compile_commands.json 文件&#xff0c;記錄使用的編譯命令 set(CMAKE_EXPORT_COMPILE_COMMANDS ON)獲得現有模塊列表 cmake --help-module-list查看命令文檔 cmake --help-command find_file查看模塊的詳細信息 cmake --help-mo…

php學習八:封裝

一&#xff1a;在php中&#xff0c;用class關鍵字來創建一個類&#xff0c;即進行封裝&#xff1b;在類里面有成員屬性和方法行為組成&#xff1a; 1.成員屬性:用關鍵字var來聲明,可以給初始值也可以不給;現在var廢棄&#xff0c;用public來聲明&#xff0c;public為共有屬性&a…

純Java JavaFX 2.0菜單

在有關JavaFX的最新文章中 &#xff0c;我集中討論了不使用JavaFX 1.x的JavaFXScript和不使用JavaFX 2.0的新FXML來使用JavaFX 2.0的新Java API 。 所有這些示例均已使用標準Java編譯器進行了編譯&#xff0c;并使用標準Java啟動 器執行。 在本文中&#xff0c;我將繼續演示使用…

設置QtreeWidget水平滾動條

轉載請注明出處&#xff1a;http://www.cnblogs.com/dachen408/p/7552603.html //設置treewidget水平滾動條 ui.treeWidget->header()->setSectionResizeMode(QHeaderView::ResizeToContents);ui.treeWidget->header()->setStretchLastSection(false);轉載于:https…

java 序列化 uid,Java中的序列化版本uid

How is Serialization id stored in the instance of the object ?The Serialization id we declare in Java is static field;and static fields are not serialized.There should be some way to store the static final field then. How does java do it ?解決方案The ser…

HTML5本地存儲

什么是Web Storage Web Storage是HTML5里面引入的一個類似于cookie的本地存儲功能&#xff0c;可以用于客戶端的本地存儲&#xff0c;其相對于cookie來說有以下幾點優勢&#xff1a; 存儲空間大&#xff1a;cookie只有4KB的存儲空間&#xff0c;而Web Storage在官方建議中為每個…

番石榴秒表

番石榴的秒表是番石榴第10版的另一個新番石榴類&#xff08;作為Optional &#xff0c;這是另一篇近期文章的主題&#xff09;。 顧名思義&#xff0c;這個簡單的類提供了一種方便地測量兩個代碼點之間經過的時間的方法。 與使用System.currentTimeMillis&#xff08;&#xff…

CF 839 E-最大團

CF 839 E Soltion: 就是怎么求最大團的問題: 以下是\(O(7000\times n^2)\)的做法 求一個最大團,然后將所有的藥水平均分配,到最大團的所有點上,計算答案. #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorit…