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 ...
随机推荐
- 每日学习心得:SQL查询表的行列转换/小计/统计(with rollup,with cube,pivot解析)
2013-8-20 1. SQL查询表的行列转换/小计/统计(with rollup,with cube,pivot解析) 在实际的项目开发中有很多项目都会有报表模块,今天就通过一个小的SQL ...
- thymeleaf之fragment
MUEAS项目,web前端采用thymeleaf作为展示层.这个view解析器,个人觉得非常不错.简单而且性能也比较好!个人觉得比JSP和freemarker之类,简单易用! 今天简单记录一下frag ...
- C# Color Table颜色对照表
.AliceBlue 240,248,255 .LightSalmon 255,160,122 .AntiqueWhite 250,235,215 .LightSeaGreen 32,178,170 ...
- HTML 元素
HTML 文档是由 HTML 元素定义的. HTML 元素 HTML 元素指的是从开始标签(start tag)到结束标签(end tag)的所有代码. 开始标签 元素内容 结束标签 <p> ...
- [Hibernate] - Query Select
测试了常用的一些HQL查询方法,具体HQL的强大可以参考: http://docs.jboss.org/hibernate/orm/3.5/reference/zh-CN/html/queryhql. ...
- 不包含适合于入口点的静态"Main"方法
学习新建项目.此问题做为笔记. 错误 1 程序“admin.exe”不包含适合于入口点的静态“Main”方法 原因:原来创建项目的时候,用的是“空项目”,我以为这样就会生成类库,实际上,一开始准备运行 ...
- RDP setting group policy
RDP setting group policy 1.Login to domain controller and go to Group Policy Management tool2.Click ...
- SQL 中delete和truncate区别
1.前者按行删除,后者直接删除数据页 2.前者可带where删除部分,后者只能删除全表 3.前者在事务日志中记录每一行的记录,后者只记录页的释放 4.前者删除后,标识技术值不重置,后者重置 5.由fo ...
- (WCF) WCF Service Hosting.
3 Options. 1. Host inside of an application. 2. Host into Windows service. 3. Host into IIS 参考: http ...
- GL_GL系列 - 预算管理分析(案例)
2014-07-09 Created By BaoXinjian