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. openstack(liberty): devstack之stack.sh分析

    学习openstack,从devstack入手,是个不错的选择.devstack中,首先需要分析stack.sh都做了些什么! 这里面涉及到了很多shell的基础知识.我就做个简单的梳理,方便后续查阅 ...

  2. c# 鼠标在控件上拖动 移动窗体 移动窗口

    #region 移动窗体 移动窗口 private Point _mousePoint; private int topA(Control cc) { if (cc == null || cc == ...

  3. Python(迭代器 生成器 装饰器 递归 斐波那契数列)

    1.迭代器 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退,不过这也没什么,因为人们很少在迭代途中往后退.另外,迭代器的一大优 ...

  4. PHP替换,只替换匹配到的第一个

    function str_replace_limit($search, $replace, $subject, $limit=-1) { if (is_array($search)) { foreac ...

  5. sublime_text3 用户配置

    { "auto_complete_triggers": [ { "characters": "", "selector" ...

  6. Linux下nl命令的用法详解

    Linux中nl命令和cat命令很像,不过nl命令会打上行号,属于比较不常用的命令,下面随小编一起来了解下这个鲜为人知的nl命令吧. nl命令在linux系统中用来计算文件中行号.nl 可以将输出的文 ...

  7. CSS3字体图标

    网址:http://icomoon.io/http://iconfont.cn/  阿里巴巴字体库 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1 ...

  8. Hive架构及Hive On Spark

    Hive的所有数据都存在HDFS中. (1)Table:每个表都对应在HDFS中的目录下,数据是经过序列化后存储在该目录中.同时Hive也支持表中的数据存储在其他类型的文件系统中,如NFS或本地文件系 ...

  9. SOA_Oracle SOA Suite and BPM Suite 11g官方虚拟机安装指南(案例)

    参考:Oracle官方 - http://www.oracle.com/technetwork/middleware/soasuite/learnmore/vmsoa-172279.html?ssSo ...

  10. AP_应付税务预扣税Withholding Tax中付款时产生预扣税(案例)

    2014-07-12 Created By BaoXinjian