转自: http://www.careercup.com/question?id=6287528252407808

问题描述:

A k-palindrome is a string which transforms into a palindrome on removing at most k characters.
Given a string S, and an interger K, print "YES" if S is a k-palindrome; otherwise print "NO".
Constraints:
S has at most 20,000 characters.
0<=k<=30
Sample Test Case#1:
Input - abxa 1
Output - YES
Sample Test Case#2:
Input - abdxa 1
Output – No

解答:懒得写了,下面这段通俗易懂,就先将就着看吧

The question asks if we can transform the given string S into its reverse deleting at most K letters.
We could modify the traditional Edit-Distance algorithm, considering only deletions, and check if this edit distance is <= K. There is a problem though. S can have length = 20,000 and the Edit-Distance algorithm takes O(N^2). Which is too slow.
(From here on, I'll assume you're familiar with the Edit-Distance algorithm and its DP matrix)
However, we can take advantage of K. We are only interested *if* manage to delete K letters. This means that any position more than K positions away from the main diagonal is useless because its edit distance must exceed those K deletions.
Since we are comparing the string with its reverse, we will do at most K deletions and K insertions (to make them equal). Thus, we need to check if the ModifiedEditDistance is <= 2*K
Here's the code:

   1:  int ModifiedEditDistance(const string& a, const string& b, int k) {
   2:      int i, j, n = a.size();
   3:      // for simplicity. we should use only a window of size 2*k+1 or 
   4:      // dp[2][MAX] and alternate rows. only need row i-1
   5:      int dp[MAX][MAX];
   6:      memset(dp, 0x3f, sizeof dp);    // init dp matrix to a value > 1.000.000.000
   7:      for (i = 0 ; i < n; i++)
   8:          dp[i][0] = dp[0][i] = i;
   9:   
  10:      for (i = 1; i <= n; i++) {
  11:          int from = max(1, i-k), to = min(i+k, n);
  12:          for (j = from; j <= to; j++) {
  13:              if (a[i-1] == b[j-1])            // same character
  14:                  dp[i][j] = dp[i-1][j-1];    
  15:              // note that we don't allow letter substitutions
  16:              
  17:              dp[i][j] = min(dp[i][j], 1 + dp[i][j-1]); // delete character j
  18:              dp[i][j] = min(dp[i][j], 1 + dp[i-1][j]); // insert character i
  19:          }
  20:      }
  21:      return dp[n][n];
  22:  }
  23:  cout << ModifiedEditDistance("abxa", "axba", 1) << endl;  // 2 <= 2*1 - YES
  24:  cout << ModifiedEditDistance("abdxa", "axdba", 1) << endl; // 4 > 2*1 - NO
  25:  cout << ModifiedEditDistance("abaxbabax", "xababxaba", 2) << endl; // 4 <= 2*2 - YES

We only process 2*K+1 columns per row. So this algorithm works in O(N*K) which is fast enough.

判断一个字符串在至多删除k个字符后是否为回文串的更多相关文章

  1. js判断一个字符串中出现次数最多的字符及次数

    最近面试总是刷到这个题,然后第一次的话思路很乱,这个是我个人思路 for循环里两个 if 判断还可以优化 var maxLength = 0; var maxStr = ''; var count = ...

  2. 删除部分字符使其变成回文串问题——最长公共子序列(LCS)问题

    先要搞明白:最长公共子串和最长公共子序列的区别.    最长公共子串(Longest Common Substirng):连续 最长公共子序列(Longest Common Subsequence,L ...

  3. mysql判断一个字符串是否包含某几个字符

    使用locate(substr,str)函数,如果包含,返回>0的数,否则返回0

  4. Codeforces Round #410 (Div. 2) A. Mike and palindrome【判断能否只修改一个字符使其变成回文串】

    A. Mike and palindrome time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  5. 疯子的算法总结(七) 字符串算法之 manacher 算法 O(N)解决回文串

    有点像DP的思想,写写就会做. #include<bits/stdc++.h> using namespace std; const int maxn=1e7+5; char a[maxn ...

  6. 最长(大)回文串的查找(字符串中找出最长的回文串)PHP实现

    首先还是先解释一下什么是回文串:就是从左到右或者从右到左读,都是同样的字符串.比如:上海自来水来自海上,bob等等. 那么什么又是找出最长回文串呢? 例如:字符串abcdefedcfggggggfc, ...

  7. 《LeetBook》leetcode题解(5):Longest Palindromic [M]——回文串判断

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  8. 判断一个字符串str不为空的方法

    1.str == null; 2."".equals(str); 3.str.length 4.str.isEmpty(); 注意:length是属性,一般集合类对象拥有的属性,取 ...

  9. C#算法之判断一个字符串是否是对称字符串

    记得曾经一次面试时,面试官给我电脑,让我现场写个算法,判断一个字符串是不是对称字符串.我当时用了几分钟写了一个很简单的代码. 这里说的对称字符串是指字符串的左边和右边字符顺序相反,如"abb ...

随机推荐

  1. jruby中的异常

    先看看ruby中的异常知识: 异常处理raise 例子: raise raise "you lose" raise SyntaxError.new("invalid sy ...

  2. EmguCV学习——简单算法 差分与高斯

    公司项目需要检测运动物体,我对opencv也没啥研究,google了好久看了好多方法,最简单的就是差分与高斯背景建模了. 旁边搞c++的同事正在搞更nb的算法,等出来了 我再转成C#版的分享. 先看差 ...

  3. equals函数

    equals函数在Object类当中,而Object类是所有类的父类,所以所有的类里面都有equals函数. “==”操作符之前用于比较两个基本数据类型的值是否相等,而对于引用数据类型,“==”操作符 ...

  4. 浅谈HAL

    参考:http://blog.csdn.net/mr_raptor/article/details/8074549 代码实现:http://blog.csdn.net/mr_raptor/articl ...

  5. CDN 内容分发网络技术

    1.前言 Internet的高速发展,给人们的工作和生活带来了极大的便利,对Internet的服务品质和访问速度要求越来越高,虽然带宽不断增加,用户数量也在不断增加,受Web服务器的负荷和传输距离等因 ...

  6. vsftpd配置文件说明

    (1)常用选项: chroot_local_user=YES #限制所有的用户均不能切换到其他目录 allow_writeable_chroot=YES #允许根目录可写 FTP的工作模式有两种,一种 ...

  7. SDOI2016 round1滚粗记

    Day -1 刚刚从HN集训回来,感觉整个人萌萌哒.考前不断立flag——这次我一定会滚粗的,然后设想着滚粗之后文化课先补什么,浑浑噩噩的过了一天,晚上看到CA爷(娘)发了关于cena爆栈的问题,这时 ...

  8. 文本阴影text-shadow

    文本阴影text-shadow text-shadow可以用来设置文本的阴影效果. 语法: text-shadow: X-Offset Y-Offset blur color; X-Offset:表示 ...

  9. Node.js 学习(六)Node.js EventEmitter

    Node.js 所有的异步 I/O 操作在完成时都会发送一个事件到事件队列. Node.js里面的许多对象都会分发事件:一个net.Server对象会在每次有新连接时分发一个事件, 一个fs.read ...

  10. CSS3 绘制360安仔小精灵[原创]

    Css3图形通常由矩形,圆形,椭圆,三角形,梯形等组合而成. 矩形,为display:block的块级元素设定宽高,便能实现, 圆角矩形,椭圆,圆形,则通过border-radius 属性来得到. 圆 ...