Kattis - names Palindrome Names 【字符串】
题目链接
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 【字符串】的更多相关文章
- Palindrome Names
Palindrome Names Kattis - names Anna and Bob are having a baby. They both enjoy the advantage of hav ...
- 【Python】回文palindrome——利用字符串反转
回文 palindrome Python 字符串反转string[::-1] Slice notation "[a : b : c]" means "count in i ...
- 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 ...
- [leetcode]131. Palindrome Partitioning字符串分割成回文子串
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- 2017年上海金马五校程序设计竞赛:Problem E : Find Palindrome (字符串处理)
Description Given a string S, which consists of lowercase characters, you need to find the longest p ...
- LeetCode 之 Valid Palindrome(字符串)
[问题描写叙述] Given a string, determine if it is a palindrome, considering only alphanumeric characters a ...
- PAT-1136(A Delayed Palindrome)字符串处理+字符串和数字间的转换
A Delayed Palindrome PAT-1136 我这里将数字转换为字符串使用的是stringstream字符串流 扩充:将字符串转换为数字可以使用stoi函数,函数头为cstdlib #i ...
- Codeforces Beta Round #7 D. Palindrome Degree —— 字符串哈希
题目链接:http://codeforces.com/contest/7/problem/D D. Palindrome Degree time limit per test 1 second mem ...
- CodeForces 7D Palindrome Degree 字符串hash
题目链接:点击打开链接 #include<stdio.h> #include<iostream> #include<string.h> #include<se ...
随机推荐
- 【BIEE】02_新建资料库并创建简单分析
一.新建资料库 1.开始→打开BI管理→点击新建资料库 2.文件→新建资料档案库 下一步 在下面的框中一次填入 连接类型:OCI 10g/11g(直接选择即可) 数据库名称:(DESCRIPTION ...
- NoSQL之Redis学习小结
大数据时代要求: 三V:Volume海量.Velocity实时.Variety多样: 三高:高并发.高可扩.高性能 高并发操作不建议使用关联查询,而使用冗余数据,分布式系统支持不了太多的并发. 横向 ...
- 将NSArray反向排序
NSArray * array = [NSArray arrayWithObjects:", nil]; NSArray * reverseArray = [[array reverseOb ...
- Word基本文档字体设置
另:段落行距选择:固定值:26/28
- 工作总结 错误 using 块缺少结束字符“}”。请确保此块内的所有“{”都有匹配的“}”字符,并且任何“}”都不会解释为标记。
页面上 有两个 它会跟标签 匹配的 标准要在同一级别下 什么也不改变 只改变它们位置 就不报错了 总结 @using (Html.BeginForm()) { } 要根据标签位置 匹配 要放 ...
- asp.net core mvc视频A:笔记3-4.母版页与部分视图
新建项目3.4, 新建一个共享文件,一般存放在Shared目录下方 选择 如果安装了Reshaper插件可以这样添加(插件在本人博客中找) 代码 创建一个空的控制器TestController 使用布 ...
- android activity声明周期学习笔记
android生命周期图: Activity继承了ApplicationContext: 1:初次加载activity时顺序执行:onCreate()-->onStart()-->onRe ...
- 已经mock类中引用的其它service类,但是在invoke私有方法的时候,该service类是空值
错误原因:没有在开始测试用例的时候,初始化类的所有注解方法. 解决方法: 使用mock方法创建mock对象时,需要在测试用例执行前执行以下代码.通常, 这句代码可以放在测试基类或者@Before 中. ...
- freemarker 开始时间与当前时间进行比较
<#if startTime?datetime lt .now?datetime>:年月日时分秒比较 <#if startTime?date lt .now?date>:年月日 ...
- (2) yum源配置-163
1.获取yum源文件 登录http://mirrors.163.com/.help/centos.html,查看CentOS6的链接地址(右键点击“CentOS6”,选择复制链接地址),链接地址为:h ...