zoukankan      html  css  js  c++  java
  • 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

    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=1008 ;
    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-1 ;
         Right=id+1 ;
         int dif=0 ;
         int ans_left_id=my_hash[id] ,ans_length=1 ;
         while(Left>=0&&Right<Len&&dif<K){
             if(str[Left]!=str[Right])
                   dif++ ;
             ans_left_id=my_hash[Left] ;
             ans_length=my_hash[Right]-my_hash[Left]+1 ;
             Left-- ;
             Right++ ;
         }
         /*k th will go on*/
         while(Left>=0&&Right<Len&&str[Left]==str[Right]){
             ans_left_id=my_hash[Left] ;
             ans_length=my_hash[Right]-my_hash[Left]+1 ;
             Left-- ;
             Right++ ;
         }
         return Node(ans_left_id,ans_length) ;
    }
    
    Node gao_case2(int id){
         int Left ,Right ;
         Left=id  ;
         Right=id+1  ;
         int dif=0 ;
         int ans_left_id=my_hash[id] ,ans_length=0 ;
         while(Left>=0&&Right<Len&&dif<K){
             if(str[Left]!=str[Right])
                   dif++ ;
             ans_left_id=my_hash[Left] ;
             ans_length=my_hash[Right]-my_hash[Left]+1 ;
             Left-- ;
             Right++ ;
         }
         /*k th will go on*/
         while(Left>=0&&Right<Len&&str[Left]==str[Right]){
             ans_left_id=my_hash[Left] ;
             ans_length=my_hash[Right]-my_hash[Left]+1 ;
             Left-- ;
             Right++ ;
         }
         return Node(ans_left_id,ans_length) ;
    }
    
    int main(){
       int L_old ,ans_len ,ans_left_id ,T=1 ;
       while(scanf("%d",&K)!=EOF){
            getchar() ;
            gets(old_str) ;
            Len=0 ;
            L_old=strlen(old_str) ;
            for(int i=0;i<L_old;i++){
                if('A'<=old_str[i]&&old_str[i]<='Z'){
                    my_hash[Len]=i+1 ;
                    str[Len++]=old_str[i]-'A'+'a' ;
                }
                else if('a'<=old_str[i]&&old_str[i]<='z'){
                    my_hash[Len]=i+1 ;
                    str[Len++]=old_str[i] ;
                }
                else
                    continue ;
            }
            str[Len]='' ;
            ans_len=0 ;
            for(int i=0;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
    ",T++ ,ans_len,ans_left_id) ;
       }
       return 0 ;
    }
  • 相关阅读:
    Postgresql主从流复制+Redis集群部署
    数据仓库实时数据同步方案
    数据库与WEB服务器的配置
    HOSTS文件
    Android 命令设置获取、IP地址、网关、dns
    转:mysqld与mysqld_safe的区别
    mysql 5.7 创建用户报错ERROR 1364 (HY000): Field 'ssl_cipher' doesn't have a default value
    MySql 5.7中添加用户,新建数据库,用户授权,删除用户,修改密码
    监控网卡设备流量
    获取进程所有信息
  • 原文地址:https://www.cnblogs.com/liyangtianmen/p/3374618.html
Copyright © 2011-2022 走看看