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. 【shell】多命令执行顺序

    :举例[root@andon ~]# date;dd if=/dev/zero of=/home/1 bs=1k count=10240;date ##统计dd命令消耗时间,其中/dev/zero为空 ...

  2. SPOJ #500. Turbo Sort

    Sorting is not an out-dated topic. My own in-place qsort got TLE... so, I simply called stl::sort() ...

  3. android学习笔记20——ProgressDialog进度条对话框

    ProgressDialog==>进度条对话框 ProgressDialog本身就代表一个进度条对话框,程序只需要创建ProgressDialog实例,并将其显示出来就是一个进度条对话框:开发者 ...

  4. FastReport使用总结三——条码简介

    FastReport Desinger中支持的Barcode类型如下图所示: 设置其Barcode属性可以实现支持不同的条码类型. 注意: 支持的条码类型说明如下: 总结: 1.通过设置Barcode ...

  5. FreeDroid开发过程中遇到的一些问题

    http://bestzp.com/?p=83 Android Studio混淆: build.gradle中   1 2 3 4 5 6 buildTypes {         release { ...

  6. (WPF, MVVM) Textbox Binding

    参考:http://msdn.microsoft.com/en-us/library/system.windows.data.updatesourcetrigger(v=vs.110).aspx Te ...

  7. 纯c++实现之滚动窗口

    别在MFC了,先分析下,上图 我们以左上角为坐标原点,用position_width和position_height来保存当前显示坐标. 根据msdn说明,滚动条默认情况下的值在0~100之间. 根据 ...

  8. zabbix安装,关闭SELinux

    一.缘由 在安装zabbix的时候,按照官网的Zabbix Manual一路跑下来,zabbix的dashboard提示:zabbix server is not running the inform ...

  9. bug_ _ java.lang.IllegalArgumentException: pointerIndex out of range 问题的两种解决办法

    ========== 4     如何解决java.lang.IllegalArgumentException: pointerIndex out of range? 今天遇到一个bug:java.l ...

  10. ylbtech-LanguageSamples-Threading(线程处理)

    ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-Threading(线程处理) 1.A,示例(Sample) 返回顶部 “线程处理”示例 ...