传送门: 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)。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int MAX_N=1e5+10;
 4 char s[MAX_N];
 5 int len;
 6 int opp(int j, int x){
 7     if(x==0){
 8         return len+1-j;
 9     }
10     if(j<x){
11         return len-j<x? len-j: len-j+1;
12     }
13     else{
14         return len+2-j;
15     }
16 }
17 bool ok(int x){
18     int tmp=x?(len-1)>>1:len>>1;
19     for(int i=0, j=1; i<tmp; i++, j++){
20         if(j==x){
21             j++;
22         }
23         if(s[j]!=s[opp(j, x)]){
24             return false;
25         }
26     }
27     return true;
28 }
29 int main(){
30     int T;
31     scanf("%d", &T);
32     while(T--){
33         scanf("%s", s+1);
34         len=strlen(s+1);
35         for(int i=0; i<=len; i++){
36             if(ok(i)){
37                 printf("%d\n", i-1);
38                 break;
39             }
40         }
41     }
42     return 0;
43 }

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

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

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

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

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

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

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

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab

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

我们不难发现一个问题

转载于:https://www.cnblogs.com/Patt/p/4467609.html

Palindrome Index相关推荐

  1. leetcode题库:5.最长回文子串Longest Palindrome string

    题目描述: /** 题目地址:https://leetcode-cn.com/problems/longest-palindromic-substring/description/  * 题目:最长回 ...

  2. E - Palindrome Numbers

    题目链接:https://vjudge.net/contest/237394#problem/E A palindrome is a word, number, or phrase that read ...

  3. LeetCode-336 Palindrome Pairs

    题目描述 Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so t ...

  4. Valid Palindrome

    一边看覃超的斗鱼直播一遍写的: in place comparison: s[i] == s[n-i-1] ? reverse and compare stack, queue 对应第一种方法: 这题 ...

  5. 超全的回文题汇总-Palindrome

    最近做了好多回文类题目,一连串发现了好多,做了如下汇总: 1.判断单链表是否是回文 - Palindrome Linked List Time Complexity: O(n), Space Comp ...

  6. LeetCode | Palindrome Partitioning I,II

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

  7. codeforce1673C. Palindrome Basis

    传送门:今天不学习,明天变辣鸡 简述一下题意:给出一个n,求有多少回文数相加的方式等于n. 其中4 = 1+2+1 和 4 = 2+1+1为一种相加方式,要求方式间至少要有一个数字不同,才能叫做不同方 ...

  8. 算法——回文(palindrome)

    回文(palindrome):指的是从头读到尾与从尾读到头一模一样的字符串. 分别在C.Java与Python实现回文检测: C: #include <stdio.h> #include ...

  9. Java Palindrome Tutorials

    Java Palindrome tutorial shows how to work with palindromes in Java. Java Palindrome教程展示了如何在Java中使用回 ...

最新文章

  1. Web应用程序运行平台的数据安全传输
  2. Docker 制作镜像的方式
  3. linux shell脚本关闭指定端口号的进程
  4. C++之临时对象、常引用和浅拷贝探究
  5. rest php,REST介绍与REST在PHP中的应用
  6. java 打包工具_Java打包Windows安装程序
  7. 如何在 Pr 中创建动作序列?
  8. 微信应用开发简单示例,学生自助报道系统
  9. windows主题Mac OS 风格 BigSur主题美化
  10. python爬虫微信_python 微信爬虫
  11. TCP报文( tcp dup ack 、TCP Retransmission)
  12. html5 摇骰子游戏,HTML5+JavaScript实现掷骰子游戏代码
  13. 笔记:线性代数的本质
  14. IDEA快捷键说明大全
  15. qq音乐html5测试性格,根据你的听歌习惯测试你的性格
  16. 图片旋转90度解决的方法
  17. R语言 devtools
  18. 新版白话空间统计(6):在ArcGIS中实现莫兰指数计算
  19. 天行健君子以自强不息----宣言
  20. matlab parfor使用

热门文章

  1. RTX5 | 信号量01 - 信号量的使用
  2. Qt文档阅读笔记-Qt 3D: Simple C++ Example解析
  3. Wireshark文档阅读笔记-TCP 4 times close解析与实例
  4. Java工作笔记-Spring boot中配置文件加密(Jasypt的使用)
  5. Arduino笔记-定时器中断(MsTimer2库使用)
  6. C++\Python\Qt工作笔记-读取txt文件查找某行是否包含keyWord
  7. Qt学习笔记-基于QGraphicsScene的填词游戏
  8. C/C++插入数据到Mysql数据库中
  9. java8的时间工具类_JAVA8日期工具类
  10. html引入vue不兼容ie11,关于vue.js:iview按需引入ie11不兼容报无效字符问题解决