题目链接

https://open.kattis.com/problems/names

题意

给出一个字符串

有两种操作

0.在字符串的最末尾加一个字符

1.更改字符串中的一个字符

求最少的操作步数使得字符串变成回文串

思路

由于回文串具有对称关系

所以给出一串回文串 最多的操作步数 就是 len / 2

只改一半不同的字符就可以了

所以我们可以先将字符串倒置

然后依次一步一步 挪过去 与原串比较 比出有多少个不同的字符

那么操作次数就是 挪位数+不同字符个数/ 2

比如说

样例

kaia

aiak 挪位数 0 不同字符个数 4 总操作数 2

kaia

aiak 挪位数 1 不同字符个数 0 总操作数1

就是答案了

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <list>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits> #define CLR(a, b) memset(a, (b), sizeof(a))
#define pb push_back
#define bug puts("***bug***");
//#define gets gets_s using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <string, int> psi;
typedef pair <string, string> pss;
typedef pair <double, int> pdi; const double PI = acos(-1.0);
const double E = exp(1.0);
const double eps = 1e-8; const int INF = 0x3f3f3f3f;
const int maxn = 1e4 + 10;
const int MOD = 1e9 + 7; int main()
{
string s;
getline(cin, s);
string tmp = s;
reverse(s.begin(), s.end());
int len = s.size();
int ans = len / 2;
for (int i = 0; i < len; i++)
{
int step = i;
int dif = 0;
for (int j = i, k = 0; j < len; j++, k++)
{
if (tmp[j] != s[k])
dif++;
}
dif /= 2;
ans = min(ans, dif + step);
}
cout << ans << endl;
}

Kattis - names Palindrome Names 【字符串】的更多相关文章

  1. Palindrome Names

    Palindrome Names Kattis - names Anna and Bob are having a baby. They both enjoy the advantage of hav ...

  2. 【Python】回文palindrome——利用字符串反转

    回文 palindrome Python 字符串反转string[::-1] Slice notation "[a : b : c]" means "count in i ...

  3. EnCase v7 could not recognize Chinese character folder names / file names on Linux Platform

    Last week my friend brought me an evidence file duplicated from a Linux server, which distribution i ...

  4. [leetcode]131. Palindrome Partitioning字符串分割成回文子串

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  5. 2017年上海金马五校程序设计竞赛:Problem E : Find Palindrome (字符串处理)

    Description Given a string S, which consists of lowercase characters, you need to find the longest p ...

  6. LeetCode 之 Valid Palindrome(字符串)

    [问题描写叙述] Given a string, determine if it is a palindrome, considering only alphanumeric characters a ...

  7. PAT-1136(A Delayed Palindrome)字符串处理+字符串和数字间的转换

    A Delayed Palindrome PAT-1136 我这里将数字转换为字符串使用的是stringstream字符串流 扩充:将字符串转换为数字可以使用stoi函数,函数头为cstdlib #i ...

  8. Codeforces Beta Round #7 D. Palindrome Degree —— 字符串哈希

    题目链接:http://codeforces.com/contest/7/problem/D D. Palindrome Degree time limit per test 1 second mem ...

  9. CodeForces 7D Palindrome Degree 字符串hash

    题目链接:点击打开链接 #include<stdio.h> #include<iostream> #include<string.h> #include<se ...

随机推荐

  1. 谁动了我的cpu——oprofile使用札记(转)

    引言 cpu无端占用高?应用程序响应慢?苦于没有分析的工具? oprofile利用cpu硬件层面提供的性能计数器(performance counter),通过计数采样,帮助我们从进程.函数.代码层面 ...

  2. jdk1.6 升级到 jdk1.7

    将 jdk1.6 升级到 jdk1.7 下载jdk-7u11-linux-x64.tar.gz   tar zxvf  jdk-7u11-linux-x64.tar.gz mkdir   /usr/l ...

  3. Ubuntu16.04 打开txt文件乱码

    最近遇到个小问题:Ubuntu16.04下打开txt出现乱码,倒腾下解决了这个问题,记录下来. Ubuntu16.04 默认已经安装gedit.直接双击被打开的文件默认用gedit打开,显然这种方式行 ...

  4. SQL Server中LIKE和PATINDEX的用法

    在SQL Server中,能使用通配符的只有2个:LIKE.PATINDEX. 不过LIKE支持2种通配符转义,无限制最全面:而PATINDEX只支持最简单的通配符转义([]转义),限制较多. LIK ...

  5. javascript与as3交互

    文章都是发布在github再转到这边的,这边格式可能会乱掉.博客地址:benqy.com 写在前面的废话 公司首页的flash广告,都是由第三方制作的,脚本和flash文件都是由各个广告公司独立制作, ...

  6. 网络相关系列之四:数据解析之SAX方式解析XML数据

    一.XML和Json数据的引入: 通常情况下.每一个须要訪问网络的应用程序都会有一个自己的server.我们能够向server提交数据,也能够从server获取数据.只是这个时候就有一个问题,这些数据 ...

  7. TouchSlide - 大话主席

    http://www.superslide2.com/TouchSlide/downLoad.html 首  页如何使用查看参数案例演示下载页面交流反馈SuperSlide TouchSlide - ...

  8. MongoDB入门学习(1)

    什么是MongoDB ? MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统. 在高负载的情况下,添加更多的节点,可以保证服务器性能. MongoDB 旨在为WEB应用提供 ...

  9. 解决UISlider滑块不灵敏

    由于UI给的thumbImage图片过小,默认UISlider开始拖动的手势范围只有thumbImage的大小之内. 为了解决这个问题需要创建一个子类继承于UISlider.重写其中的方法: - (C ...

  10. rsync 删除大量小文件

    3 find with delete 复制代码代码如下:  test  time find ./ -type f -delete        find ./ -type f -delete  0.4 ...