[虚拟机OA]Break a Palindrome 破坏回文串

In this challenge, you will be given a palindrome which you must modify if possible. Change exactly one character of the string to another character in the range ascii[a-z] so that the string meets the following two conditions:

• The new string is not a palindrome

• The new string is lower lexicographically (alphabetically) than the initial string.

• The new string is the lowest value string lexicographically that can be created from the original palindrome after making only one change.

If it not possible to create a string meeting the criteria, return the string IMPOSSIBLE.

Function Description

Complete the function breakPalindrome in the editor below. The function must return the resulting string or IMPOSSIBLE if one cannot be formed.
breakPalindrome has the following parameter(s):

s: the original string

Sample Case  
Sample Input For Custom Testing

bab

Sample Output

aab

Explanation The string aab is the smallest string which satisfies all the given conditions.

题意:

给定?一个palindrome。要改变?一个char,使得新的string不不是palindrome。?而且要是lexicocographically最?小的,不不然就返回"IMPOSSIBLE"。

把左半边不不是a的改成a就?行行了了。举例例:输?aba。算法算出aaa但仍是回?文字串串,所以输出IMPOSSIBLE。 输?入abba,输出aaba。

思路:

代码:

原文地址:https://www.cnblogs.com/liuliu5151/p/11509825.html

时间: 2024-10-29 13:19:26

[虚拟机OA]Break a Palindrome 破坏回文串的相关文章

Palindrome Number(回文串)

题目: Determine whether an integer is a palindrome. Do this without extra space. 分析: 该题目来源于leetcode.回文串是一个正读和反读都一样的字符串,比如"level"或者"noon"等等就是回文串.当然整数形式的回文串也是类似的.但负数不是回文串.两种思路: 按定义来,依次比较串的首尾,直到中间相隔1个或0个元素(取决于整数是奇数位数还是偶数位数).优点是当不是回文串时,可以很快发

UVa 10617 Again Palindrome(回文串区间DP)

UVa 10617 Again Palindrome(经典回文串区间DP) 题意: 给定一个字符串s,对s进行删除操作,使得剩下的子串是回文字符串,问最多有多少种这种子串. 思路: 涉及到回文字符串,首先要想到的肯定是区间DP,如何写出状态转移方程? 直接从题意切入:dp[i, j]表示区间[i, j]最多有多少个这样的子串. 1. s[i] == s[j] 去掉s[i],则一个子问题就是dp[i+1, j]; 去掉s[j],另一个子问题就是dp[i, j-1]; 显然这两个子问题是会有重叠的,

1328破坏回文串

题目:给你一个回文字符串 palindrome ,请你将其中 一个 字符用任意小写英文字母替换,使得结果字符串的字典序最小,且 不是 回文串.请你返回结果字符串.如果无法做到,则返回一个空串.链接:https://leetcode-cn.com/problems/break-a-palindrome 法一:自己的代码 思路:题目中说的字典序可以理解成‘a’对应1,‘b’对应2,而字典序最小就是字符串对应的数字值最小,如ab大于aa,因为12大于11,可以理解成1到9的数字排列成最小.所以字典序最

bzoj 3768: spoj 4660 Binary palindrome二进制回文串

Description 给定k个长度不超过L的01串,求有多少长度为n的01串S满足: 1.该串是回文串 2.该串不存在两个不重叠的子串,在给定的k个串中. 即不存在a<=b<c<=d,S[a,b]是k个串中的一个,S[c,d]是k个串中的一个 (It does not contain two non-overlapped substrings in the given list of K binary strings.) 举个例子,若给定2(k=2)个01串:101和1001 1010

HDU 1513 Palindrome 求回文串

这个题是走弯路了,刚开始自己DP出了方程,无限MLE,唉 if(s1[i]==s1[j]) dp[i][j]=dp[i+1][j-1]; else dp[i][j]=min(dp[i][j-1],dp[i+1][j]) +1; 后来百度了一下,这个原来是个经典回文串问题,即先将串置反,然后求LCS........ 然后就是这题卡时间卡的特别厉害,多用了一次strlen就TLE AC: #include<cstdio> #include<string> #include<str

Uva 10617 Again Palindrome (DP+回文串)

Problem I Again Palindromes Input: Standard Input Output: Standard Output Time Limit: 2 Seconds A palindorme is a sequence of one or more characters that reads the same from the left as it does from the right. For example, Z, TOT and MADAM are palind

POJ--3974 Palindrome(回文串,hash)

链接:点击这里 #include<iostream> #include<algorithm> #include<stdio.h> #include<cstring> using namespace std; #define maxn 1000005 #define LL long long #define ull unsigned long long const LL P = 131; ull p[maxn+10],f[maxn],ff[maxn]; int

Valid Palindrome ——判断字符串是否为回文串

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41488377 Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama&

LeetCode Valid Palindrome 有效回文(字符串)

1 class Solution { 2 public: 3 bool isPalindrome(string s) { 4 if(s=="") return true; 5 if(s.length()==1) return true; //单个字符,对称 6 char *p,*q; 7 p=&s[0]; //p指向开头 8 q=&s[s.length()-1]; //q指向末尾 9 while(p!=q){ 10 //测试字符串里是否有字母或数字,若没有,则立刻返回