题目链接

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. 【Excle数据透视表】如何在组的顶部显示分类汇总

    调整前                                                                                     调整后        例 ...

  2. ExtJs的Ext.grid.GridPanel不能选择复制表格中的内容解决方案

    今天遇到grid复制的问题,在网上找到了一个解决办法,只需改下CSS和JS,给大家分享一下: 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/dy_paradise/a ...

  3. Android Zxing 加入闪光灯功能

    近期做了关于二维码解析的模块 选用的是google的开源projectZxing 在Zxing 加入闪光灯功能 例如以下: 在 com.xxx.xxx.Zxing.camera 包下的CameraMa ...

  4. 代码设置UIButton文字、图片位置

    假设有按钮rButton的 imageEdgeInsets和contentEdgeInsets可以设置按钮的标题和图片的位置,如下代码,设置标题居右 NSString * rBtnTitle = @& ...

  5. Https单向认证和双向认证介绍

    一.Http HyperText Transfer Protocol,超文本传输协议,是互联网上使用最广泛的一种协议,所有WWW文件必须遵循的标准.HTTP协议传输的数据都是未加密的,也就是明文的,因 ...

  6. layout_gravity和gravity的区别

    受不了了,用一遍查一遍...根本记不住,来这里记录一下 layout_gravity是子view相对于父view的位置,比如说,在button中设置了layout_gravity="cent ...

  7. 红米手机连接logcat,调试信息刷屏解决办法

    我买了红米,平时拿它来当作安卓测试机的 可是把它接入eclipse里面,发现它会往logcat打印大量的日志,我自己想调试一个程序的时候,自己的程序的日志一会儿就被冲刷没了 新建一个logcat me ...

  8. window 添加环境变量

    右击我的电脑 选择属性 点选高级选项卡 点击环境变量 在系统变量中选中path变量 点击编辑 在变量值得最后插入 ;C:\Python27\  (改为Python的实际的安装地址) 记住后面要有最后面 ...

  9. 【Selenium + Python】自动化测试之发送邮件正文以及附件同时发送

    废话不多说,直接上代码: import unittest import time import os import smtplib from HTMLTestRunner import HTMLTes ...

  10. 微服务网关哪家强?一文看懂Zuul, Nginx, Spring Cloud, Linkerd性能差异

      导语:API Gateway是实现微服务重要的组件之一.面对诸多的开源API Gateway,如何进行选择也是架构师需要关注的焦点.本文作者对几个较大的开源API Gateway进行了压力测试,对 ...