当前位置:网站首页>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 :
边栏推荐
- 高数中值定理总结
- 最长公共子序列(LCS)(动态规划,递归)
- 5G VoNR+之IMS Data Channel概念
- Test interview | how much can you answer the real test interview question of an Internet company?
- DBSync新增对MongoDB、ES的支持
- 谈谈讲清楚这件事的重要性
- Error: No named parameter with the name ‘foregroundColor‘
- Operand of null-aware operation ‘!‘ has type ‘SchedulerBinding‘ which excludes null.
- Pointer and array are input in function to realize reverse order output
- 01机器学习相关规定
猜你喜欢
当 Knative 遇见 WebAssembly
Techniques d'utilisation de sublime
If you‘re running pod install manually, make sure flutter pub get is executed first.
How to choose an offer and what factors should be considered
- [email protected]映射关系问题"/>
接口间调用为什么要用json、fastjson怎么赋值的、fastjson [email protected]映射关系问题
SQL injection HTTP header injection
Decorator basic learning 02
装饰器基础学习02
offer如何选择该考虑哪些因素
AOSP ~Binder 通信原理 (一) - 概要
随机推荐
pmp真的有用吗?
np.random.shuffle与np.swapaxis或transpose一起时要慎用
npm ERR! 400 Bad Request - PUT xxx - “devDependencies“ dep “xx“ is not a valid dependency name
动态生成表格
Comparison between thread and runnable in creating threads
【问道】编译原理
2. Overview of securities investment funds
LabVIEW在打开一个新的引用,提示内存已满
Function pointer and pointer function in C language
【QT】自定义控件-Loading
When knative meets webassembly
Weebly移动端网站编辑器 手机浏览新时代
【PHP SPL笔记】
vector和类拷贝构造函数
Test interview | how much can you answer the real test interview question of an Internet company?
DBSync新增对MongoDB、ES的支持
Flask project uses flask socketio exception: typeerror: function() argument 1 must be code, not str
The most complete learning rate adjustment strategy in history LR_ scheduler
R descriptive statistics and hypothesis testing
Markdown editor