题目链接

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. 【VBA】查看当前窗口的宽与高

    打开的Excle如何使用VBA查看当前窗口的宽与高呢?代码如下: Sub 查看宽度与高度() Dim myWidth As Double Dim myHeigth As Double myWidth ...

  2. 谈谈IIS与ASP.NET管道

    作为一个Asp.Net平台开发者,非常有必要了解IIS和Asp.Net是如何结合,执行我们的托管代码,以及Asp.Net管道事件的. 本节目录 IIS 5.X IIS 6 IIS 7+ 集成模式 As ...

  3. Java 过滤所有html标签,复制文件到指定位置

    public static String filterHtml(String string) { String str = string.replaceAll("<[a-zA-Z]+[ ...

  4. C#中的里氏替换原则

    里氏转换原则 子类可以赋值给父类对象 父类对象可以强制转化为对应的子类对象 里氏替换原则直观理解就是"子类是父类",反过来就说不通了. 就像男人是人对的,但人是男人就不对了. 这样 ...

  5. POJ 3252 Round Numbers 数学题解

    Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, P ...

  6. Rserve方式连接别的服务器

    Rserve Rserve的方式,这是一个基于TCP/IP的服务器,通过二进制协议传输数据,可以提供远程连接,使得客户端语言能够调用R 既然是TCP/IP 就可以在不同的机器上运行了 事实上官网给出了 ...

  7. php 模拟get和post提交方法[解决ajax跨域问题]

    get: $url = "http://www.111cn.net /index.php?a=b&c=d&e=f&g=" . urlencode('王璐个人 ...

  8. RecyclerView Bug:IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter的解决方案(转)

    转自:RecyclerView Bug:IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter的解 ...

  9. JSP指令用来设置整个JSP页面相关的属性

    JSP 指令 JSP指令用来设置整个JSP页面相关的属性,如网页的编码方式和脚本语言. 语法格式如下: <%@ directive attribute="value" %&g ...

  10. 002android初级篇之ViewPager及PagerSlidingTabStrip listview的使用

    002android初级篇之ViewPager及PagerSlidingTabStrip listview的使用 ViewPager ViewPager类直接继承了ViewGroup类,所有它是一个容 ...