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 ...
随机推荐
- 1通过URL对象的openStream()方法能够得到指定资源的输入流。
通过URL读取网页内容 1通过URL对象的openStream()方法能够得到指定资源的输入流. 2通过输入流能够读取.訪问网络上的数据. 案例: import java.io ...
- VS2012 未找到与约束ContractName Microsoft.VisualStudio.Text.ITextDocumentFactoryService 未找到与约束ContractName,无法打开项目的解决方案 SQLyog 注册码
VS2012 未找到与约束ContractName Microsoft.VisualStudio.Text.ITextDocumentFactoryService 最近新换了系统还真是问题多多呀! ...
- sql server 集群配置
Windows server2003 + sql server2005 集群配置安装 一:环境 软硬件环境 虚拟3台windows server 2003主机.当中一台做域控DC,另外两台作为节点wi ...
- 常见的C++数学计算库
来源: https://blog.csdn.net/panhao762/article/details/55276811 作为理工科学生,想必有限元分析.数值计算.三维建模.信号处理.性能分析.仿真分 ...
- 使用SpannableString实现一个load小动画
依然是github开源项目:WaitingDots 这个项目代码不多,实现的非常easy.可是非常有意思由于动画的基本元素不是画出来的,而是使用了spannableString来实现. DotsTex ...
- 【WPF学习笔记】之WPF基础:依赖关系属性和通知
这些天来,对象似乎已经忙得晕头转向了.每个人都希望它们做这做那.Windows® Presentation Foundation (WPF) 应用程序中的典型对象会接到各种各样不同的请求:有要求绑定到 ...
- IntelliJ idea——》创建tag、删除tag
https://blog.csdn.net/weixin_43453386/article/details/83857038
- 三种光照模型的shader实现
1.Lambert模型,公式为I=Kd*Il(N*L): Shader "Custom/Lambert_A" { Properties { _Diffuse(,,,) } SubS ...
- Java获取字符串的CRC8校验码(由C程序的代码修改为了Java代码)
CRC8算法请百度,我也不懂,这里只是把自己运行成功的结构贴出来了.方法CRC8_Tab这里没有处理,因为我的程序中没有用到. package com.crc; public class CCRC8_ ...
- 查看Android.mk文件中的变量的值
当某个Android.mk中包含如下: LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_C_INCLUDES += \ $(LOCAL ...