Problem A

Almost Palindrome

Given a line of text, find the longest almost-palindrome substring. A string S is almost-palindrome if

  1. S begins and ends with a letter, and
  2. a(S) and b(S) have at most 2k positions with different characters

Here a(S) is the string after removing all non-letter characters and converting all the letters to lowercase, b(S) is the reversed string of a(S).

For example, when k=1, "Race cat" is almost-palindrome, because a(S)="racecat" and b(S)="tacecar" differ at exactly 2 positions.

Input

There will be at most 25 test cases. Each test case contains two lines. The first line is k (0<=k<=200). The second line contains a string with at least one letter and at most 1,000 characters (excluding the newline character). The string will only contain letters, spaces and other printable characters like ("," or "." etc) and will not start with a whitespace.

Output

For each test case, print the length of the longest almost-palindrome substring and its starting position (starting from 1). If there is more than one such string, print the smallest starting position.

Sample Input

1
Wow, it is a Race cat!
0
abcdefg
0
Kitty: Madam, I'm adam.

Output for the Sample Input

Case 1: 8 3
Case 2: 1 1
Case 3: 15 8

The Ninth Hunan Collegiate Programming Contest (2013)
Problemsetter: Rujia Liu
Special Thanks: Feng Chen, Md. Mahbubul Hasan

   这个题应该是由最长回文串改编而来的,最长回文串是K=0的情形,注意一个小地方,当diff=K的时候还是可以扩展的。程序应该清晰易懂,而不是来展示小聪明的。

#include <iostream>
#include <stdio.h>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
#include <map>
#include <stack>
#include <math.h>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std ;
typedef long long LL ;
const int Max_N= ;
char old_str[Max_N] ;
char str[Max_N] ;
int K ;
int my_hash[Max_N] ;
int Len ;
struct Node{
int Left_id ;
int Lenght ;
Node(){} ;
Node(int id ,int len):Left_id(id),Lenght(len){} ;
};
Node gao_case1(int id){
int Left ,Right ;
Left=id- ;
Right=id+ ;
int dif= ;
int ans_left_id=my_hash[id] ,ans_length= ;
while(Left>=&&Right<Len&&dif<K){
if(str[Left]!=str[Right])
dif++ ;
ans_left_id=my_hash[Left] ;
ans_length=my_hash[Right]-my_hash[Left]+ ;
Left-- ;
Right++ ;
}
/*k th will go on*/
while(Left>=&&Right<Len&&str[Left]==str[Right]){
ans_left_id=my_hash[Left] ;
ans_length=my_hash[Right]-my_hash[Left]+ ;
Left-- ;
Right++ ;
}
return Node(ans_left_id,ans_length) ;
} Node gao_case2(int id){
int Left ,Right ;
Left=id ;
Right=id+ ;
int dif= ;
int ans_left_id=my_hash[id] ,ans_length= ;
while(Left>=&&Right<Len&&dif<K){
if(str[Left]!=str[Right])
dif++ ;
ans_left_id=my_hash[Left] ;
ans_length=my_hash[Right]-my_hash[Left]+ ;
Left-- ;
Right++ ;
}
/*k th will go on*/
while(Left>=&&Right<Len&&str[Left]==str[Right]){
ans_left_id=my_hash[Left] ;
ans_length=my_hash[Right]-my_hash[Left]+ ;
Left-- ;
Right++ ;
}
return Node(ans_left_id,ans_length) ;
} int main(){
int L_old ,ans_len ,ans_left_id ,T= ;
while(scanf("%d",&K)!=EOF){
getchar() ;
gets(old_str) ;
Len= ;
L_old=strlen(old_str) ;
for(int i=;i<L_old;i++){
if('A'<=old_str[i]&&old_str[i]<='Z'){
my_hash[Len]=i+ ;
str[Len++]=old_str[i]-'A'+'a' ;
}
else if('a'<=old_str[i]&&old_str[i]<='z'){
my_hash[Len]=i+ ;
str[Len++]=old_str[i] ;
}
else
continue ;
}
str[Len]='\0' ;
ans_len= ;
for(int i=;i<Len;i++){
Node now=gao_case1(i) ;
if(now.Lenght>ans_len){
ans_len=now.Lenght ;
ans_left_id=now.Left_id ;
}
else if(now.Lenght==ans_len)
ans_left_id=Min(ans_left_id,now.Left_id) ;
now=gao_case2(i) ;
if(now.Lenght>ans_len){
ans_len=now.Lenght ;
ans_left_id=now.Left_id ;
}
else if(now.Lenght==ans_len)
ans_left_id=Min(ans_left_id,now.Left_id) ;
}
printf("Case %d: %d %d\n",T++ ,ans_len,ans_left_id) ;
}
return ;
}

The Ninth Hunan Collegiate Programming Contest (2013) Problem A的更多相关文章

  1. The Ninth Hunan Collegiate Programming Contest (2013) Problem F

    Problem F Funny Car Racing There is a funny car racing in a city with n junctions and m directed roa ...

  2. The Ninth Hunan Collegiate Programming Contest (2013) Problem H

    Problem H High bridge, low bridge Q: There are one high bridge and one low bridge across the river. ...

  3. The Ninth Hunan Collegiate Programming Contest (2013) Problem I

    Problem I Interesting Calculator There is an interesting calculator. It has 3 rows of button. Row 1: ...

  4. The Ninth Hunan Collegiate Programming Contest (2013) Problem J

    Problem J Joking with Fermat's Last Theorem Fermat's Last Theorem: no three positive integers a, b, ...

  5. The Ninth Hunan Collegiate Programming Contest (2013) Problem G

    Problem G Good Teacher I want to be a good teacher, so at least I need to remember all the student n ...

  6. The Ninth Hunan Collegiate Programming Contest (2013) Problem L

    Problem L Last Blood In many programming contests, special prizes are given to teams who solved a pa ...

  7. The Ninth Hunan Collegiate Programming Contest (2013) Problem C

    Problem C Character Recognition? Write a program that recognizes characters. Don't worry, because yo ...

  8. German Collegiate Programming Contest 2013:E

    数值计算: 这种积分的计算方法很好,学习一下! 代码: #include <iostream> #include <cmath> using namespace std; ; ...

  9. German Collegiate Programming Contest 2013:B

    一个离散化的简单题: 我用的是STL来做的离散化: 好久没写离散化了,纪念一下! 代码: #include<cstdio> #include<cstring> #include ...

随机推荐

  1. Android启动画面实现

    每个Android应用程序启动之后都会出现一个Splash启动界面,显示产品LOGO.公司LOGO或者开发者信息.如果应用程序启动时间比较长,那么启动界面就是一个很好的东西,可以让用户耐心等待这段枯燥 ...

  2. SPOJ #2 Prime Generator

    My first idea was Sieve of Eratosthenes, too. But obviously my coding was not optimal and it exceede ...

  3. 【1-4】jQuery代码风格-导航栏

    实现一个导航栏,单机不同的商品名称链接,显示相应的内容,同时高亮显示当前选择的商品. 实现功能如图: css: /* reset */ ;padding:0 0 12px 0;font-size:12 ...

  4. 剑指offer系列35----序列化二叉树

    [题目] * 请实现两个函数,分别用来序列化和反序列化二叉树 * 1 / \ 2 3 /\ /\4 5 6 7 * * 序列化的结果是1,2,#,#,3,4,#,7,#,#,5,#,#,. * 而反序 ...

  5. Python 处理多层嵌套列表

    >>> movies =[ "the holy grail", 1975,"terry jones",91, ["graham ch ...

  6. jQuery formValidator使用入门

    使用插件必须加载的文件 //加载jQuery类库 <script type="text/javascript" src="jquery-1.7.1.min.js&q ...

  7. [svn]svn merge

    转:http://blog.csdn.net/keda8997110/article/details/21813035 Step by Step 完成merge 目录: Branch的必要性 1.本地 ...

  8. 黄聪:如何删除wordpress登录之后wp_footer、wp_head自行加载的Open Sans字体、fonts.googleapis.com连接导致卡死的问题

    有时候在浏览自己的WordPress网站时,发现网页长时间无响应, 卡在正在连接到fonts.googleapis.com ,如下图所示: 查看网页源码时,发现Head里面有如下一段代码: <l ...

  9. Android Gradle实用技巧——APK文件名中加上SVN版本号,日期等

    有时候,我们会希望能把APK文件名上带上打包日期,打包时svn的版本号,应用版本号等.当然这些也可以手动添加,但是手动的话也未免太不优雅了,而且可能会出错. 利用Gradle,我们可以让打包出来的ap ...

  10. (C#) 发布程序,包含某些配置文件或数据文件。

    在VS2012里面,右击需要发布的Project,选择“Properties“, 在弹出的窗口里面点选”Publish“, 再点击”Application Files“, 将默认的Publish St ...