当前位置:网站首页>Longest palindrome substring (dynamic programming)
Longest palindrome substring (dynamic programming)
2022-07-07 05:09:00 【Madness makes freedom】
Longest text substring :
State Design :dp[i][j] Express str[i] To str[j] Whether the string of is a palindrome substring , Is expressed as 1, It doesn't mean 0;
State transition equation :dp[i][j]=dp[i-1][j-1](str[i]==str[j])
= 0 (str[i]!=str[j]); However, attention should be paid here ,dp The value of is based on the substring length of the string (1...len) For incremental enumeration , Instead of subscribing with characters i As base point ,j=i+k(k=1...len), Because I'm counting the later dp[i][j] when , Need to put dp[i-1][j-1] Calculate the value of , However, at this time dp[i-1][j-1] when = It has not been calculated yet ,( A brain patch , You can figure out this logic );
The border :dp[i][i]=1, dp[i][i+1]=1(str[i]==str[i+1]);
/**
Longest text substring :
State Design :dp[i][j] Express str[i] To str[j] Whether the string of is a palindrome substring , Is expressed as 1, It doesn't mean 0;
State transition equation :dp[i][j]=dp[i-1][j-1](str[i]==str[j])
= 0 (str[i]!=str[j]);
However, attention should be paid here ,dp The value of is based on the substring length of the string (1...len) For incremental enumeration ,
Instead of subscribing with characters i As base point ,j=i+k(k=1...len), Because I'm counting the later dp[i][j] when ,
Need to put dp[i-1][j-1] Calculate the value of , However, at this time dp[i-1][j-1] when = It has not been calculated yet
( A brain patch , You can figure out this logic );
The border :dp[i][i]=1, dp[i][i+1]=1(str[i]==str[i+1]);
*/
/**
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
string str;
cin >>str;
int len=str.size();
int dp[len][len]={0},ans=1;
memset(*dp,0,sizeof dp);
for(int i=0;i<len;++i)
{
dp[i][i]=1;
if(i<len-1&&str[i]==str[i+1])
{
dp[i][i+1]=1;
ans=2;
}
}
for(int l=3;l<len;++l)
{
for(int i=0,j=0;i+l-1<len;++i)
{
j=i+l-1;
if(str[i]==str[j]&&dp[i+1][j-1]==1)
{
dp[i][j]=1;
ans=l;
}
}
}
cout << ans << endl;
return 0;
}
*/ However , We can also put dp[i][j] Change the status of , So that we can output the content of the longest palindrome substring .
State Design :dp[i][j] Express str[i] To str[j] Whether the string of is a palindrome substring , If so, this value is the length of the palindrome substring ,
It doesn't mean 0;
State transition equation :dp[i][j]=dp[i-1][j-1]+2 (str[i]==str[j])
= 0 (str[i]!=str[j]);
However, attention should be paid here ,dp The value of is based on the substring length of the string (1...len) For incremental enumeration ,
Instead of subscribing with characters i As base point ,j=i+k(k=1...len), Because I'm counting the later dp[i][j] when ,
Need to put dp[i-1][j-1] Calculate the value of , However, at this time dp[i-1][j-1] when = It has not been calculated yet
( A brain patch , You can figure out this logic );
The border :dp[i][i]=1, dp[i][i+1]=2(str[i]==str[i+1]);
/**
However , We can also put dp[i][j] Change the status of , So that we can output the content of the longest palindrome substring .
State Design :dp[i][j] Express str[i] To str[j] Whether the string of is a palindrome substring , If so, this value is the length of the palindrome substring ,
It doesn't mean 0;
State transition equation :dp[i][j]=dp[i-1][j-1]+2 (str[i]==str[j])
= 0 (str[i]!=str[j]);
However, attention should be paid here ,dp The value of is based on the substring length of the string (1...len) For incremental enumeration ,
Instead of subscribing with characters i As base point ,j=i+k(k=1...len), Because I'm counting the later dp[i][j] when ,
Need to put dp[i-1][j-1] Calculate the value of , However, at this time dp[i-1][j-1] when = It has not been calculated yet
( A brain patch , You can figure out this logic );
The border :dp[i][i]=1, dp[i][i+1]=2(str[i]==str[i+1]);
*/
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
string str;
cin >>str;
int len=str.size();
int dp[len][len]={0},ans=1;
memset(dp,0,sizeof(dp));
for(int i=0;i<len;++i)
{
dp[i][i]=1;
if(i<len-1&&str[i]==str[i+1])
{
dp[i][i+1]=2;
ans=2;
}
}
for(int l=3;l<=len;++l)
{
for(int i=0,j=0;i+l-1<len;++i)
{
j=i+l-1;
if(str[i]==str[j]&&dp[i+1][j-1]!=0)
{
dp[i][j]=dp[i+1][j-1]+2;
ans=l;
}
}
}
int l=0,r=0,imax=0;
for(int i=0;i<len;++i)
for(int j=0;j<len;++j)
{
if(dp[i][j]>imax)
{
imax=dp[i][j];
l=i;
r=j;
}
}
cout << ans << endl;
cout <<imax << endl;
for(;l<=r;++l)
cout << str[l];
return 0;
}The more efficient one should be binary plus string hash How to do it , This is finished , I'll review the string again when I have time tomorrow hash, It's a little fuzzy .
When writing this code , I found a funny thing :
That is, when the array is not a global variable :
If the array size is a variable , Then the array is initialized to 0 when , The value of the element may not be 0;
If the array size is constant , Then the array is initialized to 0 when , The value of the element is 0;
/**
When the array is not a global variable :
If the array size is a variable , Then the array is initialized to 0 when , The value of the element may not be 0;
If the array size is constant , Then the array is initialized to 0 when , The value of the element is 0;
*/
/**
#include <iostream>
using namespace std;
const int maxn=10;
int main()
{
cout << " Array size is a variable :\n";
cout << " Enter two values :\n";
int n, m;
cin >> n >> m;
int temp[n][m]={0};
for(int i=0;i<n;++i)
for(int j=0;j<m;++j)
cout << temp[i][j] << ' ';
cout << "\n The array size is const Constant of type :\n";
int matr[maxn][maxn]={0};
for(int i=0;i<maxn;++i)
for(int j=0;j<maxn;++j)
cout << matr[i][j] << ' ';
cout << "\n The array size is common Constant :\n";
int a[10][10]={0};
for(int i=0;i<10;++i)
for(int j=0;j<10;++j)
cout << a[i][j] << ' ';
}
*/Here is the result of this program :

边栏推荐
- sublime使用技巧
- 【ArcGIS教程】专题图制作-人口密度分布图——人口密度分析
- Redis如何实现多可用区?
- 接口间调用为什么要用json、fastjson怎么赋值的、fastjson [email protected]映射关系问题
- Batch normalization (Standardization) processing
- Two methods of chromosome coordinate sequencing
- JS variable case
- A line of R code draws the population pyramid
- Common Oracle SQL statements
- Markdown编辑器
猜你喜欢

No experts! Growth secrets for junior and intermediate programmers and "quasi programmers" who are still practicing in Universities

Operand of null-aware operation ‘!‘ has type ‘SchedulerBinding‘ which excludes null.
[email protected] Mapping relatio"/>Why JSON is used for calls between interfaces, how fastjson is assigned, fastjson 1.2 [email protected] Mapping relatio

SQL injection - secondary injection and multi statement injection

If you‘re running pod install manually, make sure flutter pub get is executed first.

使用知云阅读器翻译统计遗传学书籍

带你遨游银河系的 10 种分布式数据库

Gavin teacher's perception of transformer live class - rasa project actual combat e-commerce retail customer service intelligent business dialogue robot microservice code analysis and dialogue experim

01 machine learning related regulations

Dynamically generate tables
随机推荐
Two methods of chromosome coordinate sequencing
Sublime tips
Redis如何实现多可用区?
拿到PMP认证带来什么改变?
最长公共子序列(LCS)(动态规划,递归)
ServiceMesh主要解决的三大痛点
基于Bevy游戏引擎和FPGA的双人游戏
How to design API interface and realize unified format return?
高数中值定理总结
U++ 游戏类 学习笔记
PMP证书有没有必要续期?
offer如何选择该考虑哪些因素
Using thread class and runnable interface to realize the difference between multithreading
sublime使用技巧
SQL injection - secondary injection and multi statement injection
U++ 元数据说明符 学习笔记
《五》表格
STM32F103实现IAP在线升级应用程序
【opencv】图像形态学操作-opencv标记不同连通域的位置
想要选择一些部门优先使用 OKR, 应该如何选择试点部门?