首页 > 其他分享 >将字符串转化为回文串,并记录方案

将字符串转化为回文串,并记录方案

时间:2024-04-03 22:24:56浏览次数:12  
标签:ch 记录 字符串 include 回文 define

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string>
#include <cmath>
#include <string.h>
#define R(x) x = read()
#define For(i, j, n) for (int i = j; i <= n; ++i)
using namespace std;
inline int read()
{
    int x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}

const int N = 1005;

char s[N], s2[N];
int f[N][N],n;


void solve()
{
    int lens=strlen(1+s);
    n=strlen(1+s);
      for (int length = 2; length <= n; ++length) {  
        for (int i = 1; i <= n - length + 1; ++i) {
            int j = i + length - 1;
            if (s[i] == s[j]) {
                f[i][j] = f[i + 1][j - 1];
            } else {
                f[i][j] = min(f[i + 1][j], f[i][j - 1]) + 1;
            }
        }
    }
    cout << f[1][n] << endl;
}

string t1, t2;

void build()
{
    int i = 1, j = n;
    while (i < j) {
        cout << i << " " << j << endl;
        if (s[i] == s[j]) {
           // cout << "1" << " " << s[i] << endl;
            t1.push_back(s[i]);
            ++i;
            --j;
        } else if (f[i][j] == f[i + 1][j] + 1) { 
            //cout << "2" << " " << s[i] << endl;
            t1.push_back(s[i]);
            ++i;
        } else { 
           // cout << "3" << " " << s[j] << endl;
            t1.push_back(s[j]);
            --j;
        }
        cout << i << " " << j << endl;
    }

    if (i == j) {
        t1.push_back(s[i]);
    }

    int l = t1.size() - 1;

    if(i > j)
        l++;

    cout << t1 << endl;

    string t2 = t1.substr(0, l);
    reverse(t2.begin(), t2.end());
    t1 += t2;
}

int main()
{
    scanf("%s", 1+s);
    solve();
    build();
    cout << t1 << endl;
    return 0;
}

如果最后i==j,说明目标串长度是奇数;否则为偶数

标签:ch,记录,字符串,include,回文,define
From: https://www.cnblogs.com/smartljy/p/18113620

相关文章

  • 2023.4 做题记录
    2023.4做题记录[NOIP2018提高组]旅行看到题目中要求\(m=n\)或\(m=n-1\),此时就应当分类讨论。①当\(m=n-1\)时:此时数据为一颗树。我们贪心的想:起始点为\(1\)的时候显然最优。对于每一个节点,在它子树内按照从小到大的顺序遍历显然最优。复杂度\(O(n\logn)\),瓶颈......
  • JavaWeb-01记录
    JWT令牌JSONWebToken作用:以json格式在各方之间安全传递信息,是数字签名的。格式:标头Header.有效载荷Payload.签名Signature前两部分用Base64编码,可以被前端翻译并理解。第三部分使用编码后的前两部分,加上一个密钥,用头部声明的加密算法进行签名,保证令牌没有被篡改。swagger生......
  • 2024.04 别急记录
    1.餐巾计划问题建图跑费用流即可:\((S,1,\inf,p)\);\(\foralli\in[1,N],(i,i+N,r_i,0)\);\(\foralli\in[1,N],(S,i+N,r_i,0)\);\(\foralli\in[1,N],(i,T,r_i,0)\);\(\foralli\in[1,N),(i,i+1,\inf,0)\);\(\foralli\in[1,N-n],(i+N,i+n,\inf,f)\);\......
  • python 解析json字符串保存到对象中
    在Python中,你可以使用内置的json模块来解析JSON字符串并保存到对象中。以下是一个简单的示例:pythonimportjson#假设你有以下的JSON字符串json_string='{"name":"Alice","age":25,"city":"NewYork"}'#使用json模块的loads方法将JSON字符串解析为Python对象(在这种情况下......
  • Python中处理JSON字段时,和如何将Python对象转换为JSON字符串
    在Python中处理JSON字段时,通常使用内置的json模块。这个模块允许你将Python对象转换为JSON字符串,以及将JSON字符串解析为Python对象。以下是一些常见的JSON字段处理操作:1.将Python对象转换为JSON字符串python复制importjson#定义一个Python字典data={  "name"......
  • 【问题记录】CCES编译报错:“[Error li1030] Can not open input file ‘libadi_sigma
    一,问题现象编译工程时,报错提示:“[Errorli1030]Cannotopeninputfile‘libadi_sigma_sharc_awc.dlb’”,“[Errorli1030]Cannotopeninputfile‘libadi_sigma_sharc_nwc.dlb’”:二,问题原因&解决方法没有安装对应的插件,安装插件:SigmaStudioForSHARC-SH-Rel2.......
  • Base64 编码的字符串转换为 Blob 对象方法
    Base64编码的字符串转换为Blob对象方法 constblob=function(data:string,mime:string){data=data.split(',')[1];data=window.atob(data);letia=newUint8Array(data.length);for(vari=0;i<data.length;i++){ia[i]=data.ch......
  • BZOJ3160万径人踪灭-回文子序列(位置对称)计数
    link:https://www.luogu.com.cn/problem/P4199写manacher看到的(其实重点并不在manacher)题意:给一个仅包含2种字母的字符串,问有多少种不同的子序列,满足:内容和位置都是对称的不能是连续的一段\(1\leqn\leq10^5\)答案=子序列个数-回文串个数,回文串用manacher跑,子序列则考虑......
  • Java代码实现带时区时间字符串转为LocalDateTime对象
    不带时区时间字符串可以使用Java8中的DateTimeFormatter类来将字符串转换为LocalDateTime对象。下面是一个示例代码:importjava.time.LocalDateTime;importjava.time.format.DateTimeFormatter;publicclassDateTimeConversionExample{publicstaticvoidmain(Stri......
  • Apple Watch 运动记录自动停止 bug All In One
    AppleWatch运动记录自动停止bugAllInOneAppleWatchS6运动记录会自动停止bugquestionshttps://discussionschinese.apple.com/thread/253879237?sortBy=besthttps://discussionschinese.apple.com/thread/251948485?sortBy=bestdemosAppleWatchS6骑行记录,......