CSU 1328 近似回文词(2013湖南省程序设计竞赛A题)
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1328
解题报告:中文题题意就不说了。还好数据不大,只有1000,枚举回文串的中心位置,然后向两边扩展,当扩展到 k 大于要求的K的时候停止扩展,不断更新最长的长度跟开始位置最小。我先做了个预处理,先求出了a(S),然后用一个数组保存了a(S)中的字符在原来的字符串中对应的位置在哪,这样便于字符串比较,而且又可以在O(1)时间得到在原来串中的长度跟开始的位置。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = ;
char tt[maxn],str[maxn];
int loc[maxn],K; char chto(char c)
{
return c >= 'A' && c <= 'Z'? (c+'a'-'A'):c;
}
void StrCmp(char* str,int i,int j,int& left,int& right)
{
int k = ,len = strlen(str);
while(i >= && j < len)
{
if(str[i] != str[j]) k++;
if(k > K)
break;
i--,j++;
}
left = i+;
right = j - ;
}
int kuoleft(int l)
{
l--;
while(l >= && !((tt[l] >= 'A' && tt[l] <= 'Z') || (tt[l] >= 'a' && tt[l] <= 'z')))
l--;
return l+;
}
int kuoright(int r)
{
r++;
int len = strlen(tt);
while(r < len && !((tt[r] >= 'A' && tt[r] <= 'Z') || (tt[r] >= 'a' && tt[r] <= 'z')))
r++;
return r - ;
}
int main()
{
int kase = ;
while(scanf("%d",&K)!=EOF)
{
getchar();
gets(tt);
int f = strlen(tt),len = ;
for(int i = ;i < f;++i)
if((tt[i] >= 'A' && tt[i] <= 'Z') || (tt[i] >= 'a' && tt[i] <= 'z'))
{
str[len] = chto(tt[i]);
loc[len] = i;
len++; //记录这个字符在原来的子串中对应的位置
}
str[len] = NULL;
int s = ,L = ;
for(int l = ;l < len;++l)
{
int left = ,right = ;
StrCmp(str,l-,l+,left,right);
int ll = loc[left],rr = loc[right];
int lt = rr - ll + ;
if(lt > L) L = lt,s = ll;
else if(lt == L && ll < s) s = ll;
if(l < len -)
{
left = right = ;
StrCmp(str,l,l+,left,right);
ll = loc[left],rr = loc[right];
lt = rr - ll + ;
if(lt > L) L = lt,s = ll;
else if(lt == L && ll < s) s = ll;
}
}
printf("Case %d: %d %d\n",kase++,L,s+);
}
return ;
}
CSU 1328 近似回文词(2013湖南省程序设计竞赛A题)的更多相关文章
- CSU 1328: 近似回文词
省赛的A题...现场都没什么人做...其实就一暴力水题......坑死了... 1328: 近似回文词 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1 ...
- CSU 1328 近似回文词【最长回文字符串(三种方法)】
输入一行文本,输出最长近似回文词连续子串.所谓近似回文词是指满足以下条件的字符串: 1. S以字母开头,字母结尾 2. a(S)和b(S)最多有2k个位置不同,其中a(S)是S删除所有非字母字符并且把 ...
- CSU 1337 搞笑版费马大定理(2013湖南省程序设计竞赛J题)
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1337 解题报告:虽然x和y的范围都是10^8,但是如果a 是大于1000的话,那么a^3 ...
- csuoj 1328: 近似回文词
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1328 1328: 近似回文词 Time Limit: 1 Sec Memory Limit: 1 ...
- csu-1328 近似回文词 和 最长回文字符串
原博文地址:http://blog.csdn.net/u012773338/article/details/39857997 最长回文子串 描述:输入一个字符串,求出其中最长的回文子串.子串的含义是: ...
- 字符串 - 近似回文词 --- csu 1328
近似回文词 Problem's Link:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1328 analyse: 直接暴力枚举每一个终点,然后枚举 ...
- CSU 1503 点到圆弧的距离(2014湖南省程序设计竞赛A题)
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1503 解题报告:分两种情况就可以了,第一种是那个点跟圆心的连线在那段扇形的圆弧范围内,这 ...
- 2012年湖南省程序设计竞赛E题 最短的名字
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1115 解题报告:输入n个字符串,让你求出可以用来区别这些字符串的最少的前缀总共有多少个字 ...
- 401 Palindromes(回文词)
Palindromes A regular palindrome is a string of numbers or letters that is the same forward as ba ...
随机推荐
- wpf comboBox取值问题
这是获取值后台代码 private void button1_Click(object sender, RoutedEventArgs e) { combBox = ...
- css007 margin padding border
css007 margin padding border 1.理解盒模型(盒模型:就是把一些东西,包括html各种标签都包含在一个 看不见的盒子里) 1/在web浏览器中任何标签都是一个盒子,内容的周 ...
- Java数据结构的特点
- Reading With Purpose: A grand experiment
Reading With Purpose: A grand experiment This is the preface to a set of notes I'm writing for a sem ...
- PHP页面跳转
PHP页面跳转一.header()函数 header()函数是PHP中进行页面跳转的一种十分简单的方法.header()函数的主要功能是将HTTP协议标头(header)输出到浏览器. header( ...
- python中,ascii,unicode,utf8,gbk之间的关系梳理
在计算机中,经常遇到编码问题,本节主要梳理下ascii,unicode,utf8,gbk 这几种编码之间的关系. ASCII 计算机中,所有数据都以0和1来表示.在一开始的时候,要表示的内容比较少,人 ...
- 学海无涯的整理Ing..........
1.文章:Understanding JavaScript Function Invocation and “this” http://yehudakatz.com/2011/08/11/unders ...
- Mini ORM——PetaPoco笔记(转)
记录一下petapoco官网博客的一些要点.这些博客记录了PetaPoco是如何一步步改进的. 目录: Announcing PetaPoco PetaPoco-Improvements PetaPo ...
- 从Microsoft.AspNet.Identity看微软推荐的一种MVC的分层架构
Microsoft.AspNet.Identity简介 Microsoft.AspNet.Identity是微软在MVC 5.0中新引入的一种membership框架,和之前ASP.NET传统的mem ...
- jsp总结
JSP 定义: 1)Java Server Page, Java EE 组件,本质上是 Servlet. 2)运行在 Web Container.接收 Http Request,生成 Ht ...