传送门: Palindrome Index

Problem Statement

You are given a string of lower case letters. Your task is to figure out the index of the character on whose removal it will make the string a palindrome. There will always be a valid solution.

In case the string is already a palindrome, then -1 is also a valid answer along with possible indices.

Input Format

The first line contains T, i.e. the number of test cases.
T lines follow, each containing a string.

Output Format

Print the position (0 index) of the letter by removing which the string turns into a palindrome. For a string, such as

bcbc

we can remove b at index 0 or c at index 3. Both answers are accepted.

Constraints 
1≤T≤20 
1≤ length of string ≤100005 
All characters are Latin lower case indexed.

Sample Input

3
aaab
baa
aaa

Sample Output

3
0
-1

Explanation

In the given input, T = 3,

  • For input aaab, we can see that removing b from the string makes the string a palindrome, hence the position 3.
  • For input baa, removing b from the string makes the string palindrome, hence the position 0.
  • As the string aaa is already a palindrome, you can output 0, 1 or 2 as removal of any of the characters still maintains the palindrome property. Or you can print -1 as this is already a palindrome.

读题时需注意:

题目中先说 “There will always be a valid solution. ”,然后才说“In case the string is already a palindrome, then -1 is also a valid answer along with possible indices.”。注意体会这句话,我们首先应注意到,即使输入的字符串S是个回文串,也可以删除某个字母使其仍为回文串。如果|S|为奇数,则删除中间那个字母,结果串仍为回文串。如果|S|为偶数则删除中间两个相等字符中的任一个,结果串也回文。

完全暴力的解法:

枚举要删除的字母,检查结果串是否回文。复杂度O(N^2)。

 #include<bits/stdc++.h>
using namespace std;
const int MAX_N=1e5+;
char s[MAX_N];
int len;
int opp(int j, int x){
if(x==){
return len+-j;
}
if(j<x){
return len-j<x? len-j: len-j+;
}
else{
return len+-j;
}
}
bool ok(int x){
int tmp=x?(len-)>>:len>>;
for(int i=, j=; i<tmp; i++, j++){
if(j==x){
j++;
}
if(s[j]!=s[opp(j, x)]){
return false;
}
}
return true;
}
int main(){
int T;
scanf("%d", &T);
while(T--){
scanf("%s", s+);
len=strlen(s+);
for(int i=; i<=len; i++){
if(ok(i)){
printf("%d\n", i-);
break;
}
}
}
return ;
}

只是这解法过于暴力,TLE。

下面就要引入这道题给我的最大启示了:

寻找有助于简化问题的必要条件

考虑一下上面的单纯暴力算法有那些冗余计算。

首先必须指出一个问题:优化算法的途径是充分考虑问题的特殊性。

其次要注意到:题目要求的是存在性判别,上面的算法枚举被删除字符的位置是无可厚非的。

接着考虑一下使上面的算法达到最坏情况的数据:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab

在这种情况下,上述算法必须枚举到最后一个字符才能确定答案。

我们不难发现一个问题

Palindrome Index的更多相关文章

  1. [hackerrank]Palindrome Index

    简单题. #include <iostream> #include <string> using namespace std; int main() { int T; cin ...

  2. LintCode Palindrome Partitioning II

    Given a string s, cut s into some substrings such that every substring is a palindrome. Return the m ...

  3. LeetCode 214 Shortest Palindrome

    214-Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding ch ...

  4. 【Leetcode】【Medium】Palindrome Partitioning

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

  5. Leetcode | Palindrome

    Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...

  6. POJ2402/UVA 12050 Palindrome Numbers 数学思维

    A palindrome is a word, number, or phrase that reads the same forwards as backwards. For example,the ...

  7. [LeetCode#266] Palindrome Permutation

    Problem: Given a string, determine if a permutation of the string could form a palindrome. For examp ...

  8. Palindrome Pairs 解答

    Question Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, ...

  9. Reverse Integer - Palindrome Number - 简单模拟

    第一个题目是将整数进行反转,这个题实现反转并不难,主要关键点在于如何进行溢出判断.溢出判断再上一篇字符串转整数中已有介绍,本题采用其中的第三种方法,将数字转为字符串,使用字符串比较大小的方法进行比较. ...

随机推荐

  1. SQL SERVER中的OLEDB等待事件

    OLEDB等待事件介绍 OLEDB等待类型是SQL SERVER 数据库中最常见的几种等待类型之一.它意味着某个会话(SPID)通过SQL Server Native Client OLEDB Pro ...

  2. SQL SERVER 2014 Agent服务异常停止案例

    生产环境一数据库服务器(SQL Server 2014)的Agent服务突然停掉了,检查了错误日志,发现在"SQL Server Agent"里面没有"SQLServer ...

  3. EasyUI 中点击取消按钮关闭Dialog(对话框窗口)

    <div id="dl1" class="easyui-dialog" title="窗口"style="width:400 ...

  4. 烂泥:haproxy学习之手机规则匹配

    本文由ilanniweb提供友情赞助,首发于烂泥行天下 想要获得更多的文章,可以关注我的微信ilanniweb. 今天我们来介绍下有关haproxy匹配手机的一些规则配置. 一.业务需要 现在根据业务 ...

  5. CSS3动画事件

    CSS3 的动画效果强大,在移动端使用广泛,动画执行开始和结束都可以使用JS来监听其事件. animationstart animationend 以下是一个示例 <!DOCTYPE html& ...

  6. 深入java集合系列文章

    搞懂java的相关集合实现原理,对技术上有很大的提高,网上有一系列文章对java中的集合做了深入的分析, 先转载记录下 深入Java集合学习系列 Java 集合系列目录(Category) HashM ...

  7. linux mint17.2 安装fcitx输入法

    mint17刚出的时候,曾经在虚拟机上体验过. 现在决定好好学习linux,再加上实在是太萌mint,就在虚拟机上安装了mint17.2 开始配置fcitx输入法: 添加ppa: sudo add-a ...

  8. 【Windows编程】系列第九篇:剪贴板使用

    上一篇我们学习了常见的通用对话框,本篇来了解剪贴板的使用,它常用于复制粘贴功能. 剪贴板是Windows最早就加入的功能,由于该功能非常实用,我们几乎每天都会使用到.通过剪贴板,我们就可以将数据从一个 ...

  9. 4412开发板学习笔记-NFS服务器的搭建

    转自iTOP-4412讨论群: http://www.topeetboard.com 先来介绍一下NFS: NFS 是Network File System的缩写,即网络文件系统.一种使用于分散式文件 ...

  10. 第16章 List集合的总结和遍历

    第16章 List集合的总结和遍历 1.重构设计 根据Vector类,ArrayList类,和LinkedList类所具有的存储特点以及拥有的方法入手,发现共性往上抽取. 共同特点: 1.允许元素重复 ...