The Ninth Hunan Collegiate Programming Contest (2013) Problem A
Problem A
Almost Palindrome
Given a line of text, find the longest almost-palindrome substring. A string S is almost-palindrome if
- S begins and ends with a letter, and
- 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的更多相关文章
- 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 ...
- 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. ...
- 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: ...
- 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, ...
- 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 ...
- 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 ...
- The Ninth Hunan Collegiate Programming Contest (2013) Problem C
Problem C Character Recognition? Write a program that recognizes characters. Don't worry, because yo ...
- German Collegiate Programming Contest 2013:E
数值计算: 这种积分的计算方法很好,学习一下! 代码: #include <iostream> #include <cmath> using namespace std; ; ...
- German Collegiate Programming Contest 2013:B
一个离散化的简单题: 我用的是STL来做的离散化: 好久没写离散化了,纪念一下! 代码: #include<cstdio> #include<cstring> #include ...
随机推荐
- MyEclipse Hibernate 学习总结
最近在学习Hibernate,先把学习的过程记录一下,方便自己以后复习. 1.使用工具 MyEclipse 10 2. 1)新建Java程序 2)右键程序,选择MyEclipse-> Add H ...
- IntelliJ IDEA中如何设置同时打开多个文件且分行显示?
Window→Editor Tabs→Tabs Placement→Show Tabs in Single Row 取消选中后即可在多行显示 下图为实际显示效果: 还可以自行设置打开文件窗口数(默认 ...
- android学习笔记19——对话框(DatePickerDialog、TimePickerDialog)
DatePickerDialog.TimePickerDialog ==> DatePickerDialog.TimePickerDialog功能.用法都比较简单,操作步骤: 1.通过new关键 ...
- 【转】C#中判断扫描枪输入与键盘输入
提出问题:在收货系统中,常常要用到扫描枪扫描条码输入到TextBox,当条码无法扫描时,需要手工输入.如果是扫描枪输入时,我们将自动去判读条码,而手工输入时,最终需要加按回车键确认后判读条码.这时候我 ...
- Filter 过滤器
1. config in web.xml 2. @Component 3. @WebFilter (filterName="LoginFilter", url-patterns= ...
- BEvent_客制化BusinessEvent通过PLSQL Procedurer接受消息传递(案例)
2014-06-27 Created By BaoXinjian
- php之form表单
<!DOCTYPE HTML> <html> <head> <title>form</title> <style type=" ...
- HTML5 Web Storage概述
Web Storage html5新增功能 可以在客户端本地保存数据 之前是使用Cookies在客户端保存注入用户名等简单用户信息,但永久数据存在几个问题 大小:cookies大小被限制在4KB 带宽 ...
- 手动编译Jsp文件
手动模拟Tomcat编译jsp文件 Tomcat编译jsp文件的配置路径是在%tomcat_home%/conf/web.xml中,有这样一段代码 <servlet> <servle ...
- git添加标签(转载)
From:http://git-scm.com/book/zh/v1/Git-%E5%9F%BA%E7%A1%80-%E6%89%93%E6%A0%87%E7%AD%BE 打标签 同大多数 VCS 一 ...