Palindrome

字符串科技:Palindrome Series 文章目录简介背景知识弱周期定理Border SeriesPalindrome SeriesPAM例题:CF 932G题意题解暴力DP使用Palindrome Series优化DP符号约定转移方法总结代码第二个题: 2019 ICPC 沈阳 MNote题意:题解:代码: 简介 其实不太清楚这个应该叫什么,知道的同学可以麻烦告知我。 众所周知,一个串的border数量是O(n)O(n)O(n)的,但是... 阅读详情

A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string, determines the minimal number of characters to be inserted into the string in order to obtain a palindrome. 

As an example, by inserting 2 characters, the string "Ab3bd" can be transformed into a palindrome ("dAb3bAd" or "Adb3bdA"). However, inserting fewer than 2 characters does not produce a palindrome. 

Input

Your program is to read from standard input. The first line contains one integer: the length of the input string N, 3 <= N <= 5000. The second line contains one string with length N. The string is formed from uppercase letters from 'A' to 'Z', lowercase letters from 'a' to 'z' and digits from '0' to '9'. Uppercase and lowercase letters are to be considered distinct.

Output

Your program is to write to standard output. The first line contains one integer, which is the desired minimal number.

Sample Input

5
Ab3bd

Sample Output

2

题意:题中给了一个长度为n的字符串,求至少再加上几个字符是它变成回文字符串。

思路:我们先求出字符串a的逆序字符串b,然后求这两个字符串的最长公共子序列,所求的公共子序列就是已经匹配好的字符,还剩下n-dp[n][n]没有匹配成功,然后在相应位置上加上对应字符即可,需要注意的是,这道题数组dp不能写成 int 型,会内存超限的,应写成unsigned short型,因为unsigned shrot占字节小,代码如下:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char a[5005],b[5005];
unsigned short dp[5005][5005]; //unsigned short 无符号短整型,数值范围为:0~65535;
                               //int 有符号基本类型,数值范围为::-32768~32767。
int main()
{
    int n,i,j;
    memset(dp,0,sizeof(dp));
    scanf("%d",&n);
    scanf("%s",a);
    for(j=0;j<n;j++)
        b[j]=a[n-j-1];  // b是a的逆序
    for(i=0;i<n;i++)    //求a,b最长公共子序列
    {
        for(j=0;j<n;j++)
        {
            if(a[i]==b[j])
                dp[i+1][j+1]=dp[i][j]+1;
            else
                dp[i+1][j+1]=max(dp[i][j+1],dp[i+1][j]);
        }
    }
    printf("%d\n",n-dp[n][n]);  //n减最长公共子序列即可算出
    return 0;
}

 

【Gazebo安装指南】Ubuntu22.04LTS环境下的高效配置与常见问题解决 本文提供了在Ubuntu 22.04 LTS系统上高效安装和配置Gazebo仿真环境的详细指南。针对Gazebo Classic版本,文章对比了通过ROS 2仓库和Ubuntu官方仓库两种主流安装方法,并重点解决了安装过程中常见的软件包定位、模型下载及图形驱动等问题,帮助用户快速搭建稳定的机器人仿真开发环境。 阅读详情

相关推荐

MuJoCo 相机图片怎么拿?视角调整获取物体图片及实时显示(附代码)

在Mujoco的仿真中,对于识别抓取场景来说,如何获得添加的body等物体的实时图像很重要,今天就分享下如何添加相机视角及可视化小方块(举例)的实时图片。复制一个scene.xml为scene_withcamera.xml,在xml中添加camera和小方块如下。设置xml文件中添加的相机body id。需要创建一个离屏渲染器。

darcsdn 2135

Palindrome POJ - 1159(回文字符串 最长公共子序列 滚动数组)

回文字符串 最长公共子序列 滚动数组

riversu 439

UE4入门实例17(Unreal制作卡通Post Process材质球)

效果展示 #1 创建Post Porcess材质球 #2 编写Shader功能 #3 设置场景区域内风格化渲染 #1 创建Post Porcess材质球 #2 编写Shader功能 创建基础效果 移除现有通道渲染 设置视图渲染范围 #3 设置场景区域内风格化渲染 #4 加群下载Shader源码:284105567 ...

Unity3d 攻城师一枚 1354

POJ - 1159 Palindrome (回文串与最长公共子序列问题)

Problem Description A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string, determi...

我の名字 369

智能解决装箱问题:使用优化算法实现高效包装

装箱问题(Bin Packing Problem)是组合优化领域中的一个经典问题,主要涉及如何将一系列对象高效地装入有限数量的容器(或“箱”)中,同时满足特定的约束条件。这个问题的目标是最小化所需使用的箱子数量或者最大化箱子的装载效率,以减少空间或资源的浪费。

MindOpt_003的博客 5564

win11下的MySQL安装

目前官方最新版本是8.0.26,考虑到其稳定性、可靠性还需一定周期保证,所以使用官方版求稳定仍然建议5.7系列。3、选择MySQL Servers 5.7.38 -x64,然后按绿色箭头将选中的版本移植到右边选中框中。4、点击下方蓝色Advanced Options,选择安装位置。1、在开始界面搜索MySQL,点击command line。4、在“系统变量”中选择“path”,点击编辑。5、新建——>增加MySQL路径——>确定,12、execute,安装好后next。9、不需要做修改,直接点击next。.

rensweet的博客 2万+

动态规划之回文串问题

一.DP求回文串[POJ-1159] A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string, determines the minimal number of characters to be inserted into

逐梦er的博客 2850

科研指南:IEEE期刊LaTeX/Word模板下载与使用全解析

本文详细解析了IEEE期刊LaTeX/Word模板的下载与使用技巧,帮助科研人员高效完成论文排版。从官方模板获取入口到LaTeX文档结构配置,再到Word样式管理与常见问题解决方案,全面覆盖投稿准备的关键步骤。特别针对数学公式、参考文献和图片处理等难点提供实用建议,助力学术写作规范化。

weixin_28396629的博客 575

POJ-1159 Palindrome

最长公共子序列:https://www.cnblogs.com/wkfvawl/p/9362287.html A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a pr...

XYBDX的博客 212

图像传感器与信号处理——详解CCD与CMOS图像传感器

一文详解CCD与CMOS图像传感器一文详解CCD与CMOS图像传感器1. 图像传感器性能关键词1.1 噪声1.2 暗电流 一文详解CCD与CMOS图像传感器 本文主要总结了CCD与CMOS传感器的原理知识,参考书籍为《数码相机中图像传感器和信号处理》,英文版为《Image Sensors And Signal Processing for Digital Still Cameras》 1. 图像传...

weixin_44580210的博客 2万+

风控IV、AUC、KS python计算代码

df_temp:数据集,Y:是否逾期标签,feas_list:需要计算IV的 变量。2、 按月和不同Y 下的 IV计算。

暮雨听轩的专栏 1627

(POJ 1159)Palindrome <最长公共子序列 / 滚动数组优化 / 记忆化搜索> 简单回文数

Palindrome DescriptionA palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string, deter

STILLxjy 815

vue2/3 - H5 移动端网页中调用手机摄像头扫描二维码 / 条形码功能,在h5手机网页里调起相机摄像头进行二维码或条形码扫描效果详细教程(详细示例组件源码,一键复制开箱即用!)

vue扫描二维码条形码,vue网页调用摄像头扫二维码条形码,vue移动端如何实现调用相机扫描二维码,vue移动端扫一扫功能,vue怎么做移动端h5扫一扫功能,vue识别二维码和条形码,vue实现点击按钮调用摄像头扫码,vue调用本地摄像头实现拍照功能,Vue中如何进行二维码生成与扫描,如何实现vue在手机浏览器中调用摄像头实现扫描二维码功能,VUE项目移动端H5调用摄像头扫码,vue项目中实现H5调取摄像头,网页调用摄像头实现二维码扫描功能,vue调用手机摄像头,vue在调用摄像头扫码,vue自定义打开摄像

✅ 精选推荐文章 1万+

LeetCode Palindrome Partitioning

原题链接在这里:https://leetcode.com/problems/palindrome-partitioning/ 本题与Word Break II相似。 用递归解决子问题,如果是Palindrome就加进item,然后递归调用helper, 递归终止条件是能正好走到s.length(). Note: 1. 用的item是一个list, 实际上是一个指针, 所以递归调用完需要回朔,

Dylan_Java_NYC的博客 386

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

Simple Palindrome

Narek has to spend 2 hours with some 2-year-old kids at the kindergarten. He wants to teach them competitive programming, and their first lesson is about palindromes.Narek found out that the kids only know the vowels of the English alphabet (the letters a\

Pretty Boy Fox的博客 1115

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

LeetCode Palindrome Partitioning II

将一个字符串分割成若干个子字符串,使得子字符串都是回文字符串,要求最少需要几次分割能够满足需求。

DRFish 4015

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

1354. Palindrome. Again Palindrome

1354. Palindrome. Again Palindrome Time limit: 1.0 second Memory limit: 64 MB Awordis the nonempty sequence of symbolsa1a2…an. Apalindromeis the worda1a2…anthat is read from the left to t...

weixin_34123613的博客 256

s-palindrome

Let’s call a string “s-palindrome” if it is symmetric about the middle of the string. For example, the string “oHo” is “s-palindrome”, but the string “aa” is not. The string “aa” is not “s-palindrome”...

博客标题不能为空我也没办法 480

s-palindrome (模拟)

Let's call a string "s-palindrome" if it is symmetric about the middle of the string. For example, the string "oHo" is "s-palindrome", but the string "aa" is not. The string "aa" is not "s-palindrome&q

red_red_red的博客 279

POJ 3974 Palindrome

POJ 3974 Palindrome Description Andy the smart computer science student was attending an algorithms class when the professor asked the students a simple question, “Can you propose an efficient algorit...

旺崽的博客 2427

Palindrome(POJ 1159)

C - Palindrome Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1159 Appoint description:  System Crawler  (2015-02-13) Descript

luckyrass的专栏 439

Leetcode之Valid Palindrome(Palindrome 专题)

专题:Palindrome 原题: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama" is a palindrom

396

B. s-palindrome(模拟)

CF链接:http://codeforces.com/contest/691/problem/B Let's call a string "s-palindrome" if it is symmetric about the middle of the string. For example, the string "oHo" is "s-palindrome", but the string ...

cherish_lin的博客 328

汇编综合应用(1) Test Palindrome

在粗略的看过string instruction之后, 综合了先前所有看过的,写了个测试palindrome的程序,再次让大脑到达兴奋点,现在终于可以让大脑轻松一下了。呵呵~~~ 源码如下: .model small.stack 300h.data str1 byte 255 dup(?) str2 byte 255 dup(?) s1 byte "Please input a strin

1033

[LC] 336. Palindrome Pairs

这一题有着最straight forward的做法。就是把每俩个字符串组装一下然后检查一下是否是Palindrome。思路非常明白。代码如下: public List<List<Integer>> palindromePairs(String[] words) { List<List<Integer>> result ...

chaochen1407的专栏 516
上一篇: Free Candies
下一篇: Two Strings Swaps
Angeliaaa
博客等级 码龄8年 17粉丝 90原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值