当前位置:网站首页>1222. Password dropping (interval DP, bracket matching)
1222. Password dropping (interval DP, bracket matching)
2022-07-02 01:45:00 【seez】
It can be seen that it is a bracket match / Template question of the longest palindrome subsequence
initialization : All defined as 0
The border : Because a single character is also a palindrome subsequence ,dp[i][i]=1
State calculation / subset division : According to whether the characters at both ends are in the longest palindrome subsequence , It can be divided into the following four subsets without repetition and leakage
- s[i] and s[j] All in palindrome subsequence dp[i][j]=max(dp[i+1][j-1]+2)
- s[i] In palindrome subsequence dp[i][j]=max(dp[i][j-1])
- s[j] In palindrome subsequence dp[i][j]=max(dp[i+1][j])
- s[i]s[j] Are not in the palindrome subsequence dp[i][j]=max(dp[i-1][j-1])
According to the state representation ,si In palindrome subsequence , It can be considered as a collection dp[i][j-1] in
#include <iostream>
#include <algorithm>
using namespace std;
const int N=1005;
int dp[N][N];
int main()
{
string s;
cin>>s;
s=" "+s;
int n=s.size();
for(int i=1;i<=n;i++)
dp[i][i]=1;
for(int len=2;len<=n;len++)
for(int i=1;i+len-1<=n;i++)
{
int j=i+len-1;
dp[i][j]=max(dp[i+1][j],dp[i][j-1]);
if(s[i]==s[j])
dp[i][j]=dp[i+1][j-1]+2;
}
cout<<n-dp[1][n]-1;
return 0;
}
边栏推荐
- Quatre stratégies de base pour migrer la charge de travail de l'informatique en nuage
- Just using the way and method of consuming the Internet to land and practice the industrial Internet will not bring long-term development
- No converter found for return value of type: class
- Modeling essays series 124 a simple coding method
- [IVX junior engineer training course 10 papers] 06 database and services
- 遷移雲計算工作負載的四個基本策略
- There are spaces in the for loop variable in the shell -- IFS variable
- 技术大佬准备就绪,话题C位由你决定
- Should enterprises choose server free computing?
- [image enhancement] vascular image enhancement based on frangi filter with matlab code
猜你喜欢
How can I batch produce the same title for the video?
k线图形态这样记(口诀篇)
ES6 new method of string
[IVX junior engineer training course 10 papers] 05 canvas and aircraft war game production
企业应该选择无服务器计算吗?
Learn about servlets
Study note 2 -- definition and value of high-precision map
6-2 vulnerability exploitation - inevitable problems of FTP
5g/4g pole gateway_ Smart pole gateway
SAP ui5 beginner tutorial 20 - explanation of expression binding usage of SAP ui5
随机推荐
Luogu p1775 stone merger (weakened version)
遷移雲計算工作負載的四個基本策略
The role of artificial intelligence in network security
Modeling essays series 124 a simple coding method
Implementation of Weibo system based on SSM
Develop those things: how to use go singleton mode to ensure the security of high concurrency of streaming media?
浅浅了解Servlet
Feature extraction and detection 16 brisk feature detection and matching
电子协会 C语言 1级 32、计算2的幂
Based on configured schedule, the given trigger will never fire
Using tabbar in wechat applet
The author is more willing to regard industrial Internet as a concept much richer than consumer Internet
k线图形态这样记(口诀篇)
No converter found for return value of type: class
The difference between new and malloc
Finally got byte offer, 25-year-old inexperienced experience in software testing, to share with you
Quatre stratégies de base pour migrer la charge de travail de l'informatique en nuage
Learn basic K-line diagram knowledge in three minutes
Unity AssetBundle subcontracting
uTools