Palindrome

说到 Palindrome ,wo一般会想到构造,马拉车,dp.
这里我们看一下和dp有关的Palindrome

1.判断一个字符串是否是回文串
dp[i][j]:下标从i到j是否为回文串
状态转移方程为:

if(s[i]==s[j])
dp[i][j]=dp[i+1][j-1];
else dp[i][j]=0;

2. Cheapest Palindrome POJ - 3280
题意:给你一串字符串,可以插入或者删除,将之变为回文数。插入和删除每个字符都有代价,问你代价和最小的为多少。
d[i][j]:从i到j为回文串花费的最小
状态转移方程:
if(s[i]==s[j]) d[i][j]=d[i+1][j-1];
else d[i][j]=min(d[i][j-1]+add[s[j]],d[i+1][j]+add[s[i]],del[s[i]]+d[i+1][j],del[s[j]]+d[i][j-1]);

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>

using namespace std;
#define INF 0x3f3f3f3f
#define Min(a,b) (a)>(b)?b:a
const int maxn = 2005;
char s[maxn];
int dp[maxn][maxn];
int costa[30];
int costd[30];
int n,m;
void solve()
{
    for(int len=1;len<=m;len++)
    {
        for(int i=0;i<m;i++)
        {
            int j=i+len-1;
            if(j>=m) break;
            if(len==1) dp[i][j]=0;
            else if(s[i]==s[j]) {if(len==2) dp[i][j]=0;else dp[i][j]=dp[i+1][j-1];}
            else dp[i][j]=Min(Min(dp[i][j-1]+costa[s[j]-'a'],dp[i+1][j]+costa[s[i]-'a']),Min(costd[s[i]-'a']+dp[i+1][j],costd[s[j]-'a']+dp[i][j-1]));
        }
    }
    printf("%d\n",dp[0][m-1]);
}
int main()
{
    memset(dp,INF,sizeof(dp));
    scanf("%d %d",&n,&m);
    scanf("%s",s);
    for(int i=0;i<n;i++)
    {
        char c;
        cin>>c;
        scanf("%d %d",&costa[c-'a'],&costd[c-'a']);
    }
    solve();
    return 0;
}
/*
3 4
aaaa
a 1000 1100
b 350 700
c 200 800
*/

循环还可以这样写(不用初始化dp,快很多)

 for(int j=0;j<m;j++)//枚举串的尾
    {
        for(int i=j-1;i>=0;i--)//往前扫
        {
            if(s[i]==s[j]) dp[i][j]=dp[i+1][j-1];
            else
            {
                dp[i][j]=dp[i+1][j]+min(costa[s[i]-'a'],costd[s[i]-'a']);
                dp[i][j]=min(dp[i][j],dp[i][j-1]+min(costa[s[j]-'a'],costd[s[j]-'a']));
            }
        }
    }

* Palindrome (回文数) poj 1159 LCS*
题意:给你一串字符串,问你最少加多少个字符可以让它变为回文数。
和上题差不多的思想
dp[i][j]:从i到j为回文串的最小代价
状态转移为:
if(s[i]==s[j]) dp[i][j]=dp[i+1][j-1];
else dp[i][j]=1+min(dp[i+1][j],dp[i][j-1]);
看一下范围,是5000,如果是int类型的二维数组,会MLE。但是这里的数范围也是5000,可以用short~

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>

using namespace std;
#define INF 0x3f3f
const int maxn = 5005;
char x[maxn];
short dp[maxn][maxn];
int n;
void solve()
{
    for(int len=1;len<=n;len++)
    {
        for(int i=0;i<n;i++)
        {
            int j=i+len-1;
            if(j>=n) break;
            if(len==1) dp[i][j]=0;
            else if(x[i]==x[j]) {if(len==2) dp[i][j]=0;else dp[i][j]=dp[i+1][j-1];}
            else dp[i][j]=1+min(dp[i][j-1],dp[i+1][j]);
        }
    }
    cout<<dp[0][n-1]<<endl;
}
int main()
{
    memset(dp,INF,sizeof(dp));
    scanf("%d",&n);
    scanf("%s",x);
    solve();
    return 0;
}

还有一种做法
ans=字符串的长度-字符串和它的反向字符串的最大公共子序列的长度

因为字符串和它的反向字符串的最大公共子序列的长度k,就等同于在原串中有k个字符是对应相等的。答案就是那些对应不相等的在对应位置添加相同的字符。所以ans=n-k;

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>

using namespace std;
#define INF 0x3f3f3f3f
const int maxn = 5005;
char x[maxn];
char y[maxn];
int dp[5][maxn];
int n;
void solve()
{
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(x[i]==y[j]) dp[(i+1)%2][j+1]=dp[i%2][j]+1;
            else dp[(i+1)%2][j+1]=max(dp[i%2][j+1],dp[(i+1)%2][j]);
        }
    }
    printf("%d\n",n-dp[n%2][n]);
}
int main()
{
    scanf("%d",&n);
    scanf("%s",x);
    int h=0;
    for(int i=n-1;i>=0;i--)
        y[h++]=x[i];
    solve();
    return 0;
}

codeforces 835D

题意:定义k阶回文串的左边右边为k-1阶回文串,要求所有阶回文串的个数

分析:dp[i][j]:下标从i到j的字符串的阶数。
先判断s[i][j]是不是回文串。如果不是的话 dp[i][j]=0;是的话 dp[i][j]=1+dp[i][i+(j-i+1)/2-1];

这里判断回文串可以用dp,或者马拉车,或者哈希(这个不熟==)

这里是用dp来判断回文的

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

#define LL long long
#define inf 1000000007

const int maxn = 5005;
char s[maxn];
int dp[maxn][maxn];
int p[maxn][maxn];
int num[maxn];
int main()
{
    scanf("%s",s);
    int slen=strlen(s);
    for(int i=1;i<=slen;i++)//
    {
        for(int j=0;j<slen;j++)
        {
            int bg=j,ed=j+i-1;
            if(i==1) p[bg][ed]=1;
            else if(s[bg]==s[ed]){if(i==2)p[bg][ed]=1;else p[bg][ed]=p[bg+1][ed-1];}
        }
    }
    for(int len=1;len<=slen;len++)
    {
        for(int i=0;i<slen;i++)
        {
            int j=i+len-1;
            if(j>=slen) break;
            int mid=i+(j-i+1)/2-1;
            if(p[i][j])
                dp[i][j]=dp[i][mid]+1;
        }
    }
    num[1]=slen;
    for(int i=0;i<slen;i++)
        for(int j=i+1;j<slen;j++)
            if(dp[i][j]) num[dp[i][j]]++;
    for(int i=slen;i>=1;i--)
        num[i-1]+=num[i];
    for(int i=1;i<=slen;i++)
        printf("%d\n",num[i]);
    return 0;
}
### XTUOJ Perfect Palindrome Problem Analysis For the **Perfect Palindrome** problem on the XTUOJ platform, understanding palindromes and string manipulation algorithms plays a crucial role. A palindrome refers to a word, phrase, number, or other sequences of characters which reads the same backward as forward[^1]. The challenge typically involves checking whether a given string meets specific conditions to be considered a perfect palindrome. In many similar problems, preprocessing steps such as converting all letters into lowercase (or uppercase) can simplify subsequent checks by ensuring case insensitivity during comparison operations. Additionally, removing non-alphanumeric characters ensures that only relevant symbols participate in determining if the sequence forms a valid palindrome[^2]. To determine if a string is a perfect palindrome, one approach iterates from both ends towards the center while comparing corresponding elements until reaching the midpoint without encountering mismatches: ```python def is_perfect_palindrome(s): cleaned_string = ''.join(char.lower() for char in s if char.isalnum()) left_index = 0 right_index = len(cleaned_string) - 1 while left_index < right_index: if cleaned_string[left_index] != cleaned_string[right_index]: return False left_index += 1 right_index -= 1 return True ``` This function first creates `cleaned_string`, stripping away any irrelevant characters and normalizing cases. Then through iteration with two pointers moving inward simultaneously (`left_index` starting at position 0 and `right_index` initially set to the last index), comparisons occur between pairs of opposing positions within the processed input string. If every pair matches perfectly throughout this process, then the original string qualifies as a "perfect palindrome".
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值