• HDU 3294 (Manacher) Girls' research


    变形的求最大回文子串,要求输出两个端点。

    我觉得把'b'定义为真正的'a'是件很无聊的事,因为这并不会影响到最大回文子串的长度和位置,只是在输出的时候设置了一些不必要的障碍。

    另外要注意一下原字符串s1中的字符在预处理以后的字符串s2中对应的坐标关系,这样输出的时候就可以照着这个关系转化。

    轻松1A,嘿嘿

     1 //#define LOCAL
     2 #include <iostream>
     3 #include <cstdio>
     4 #include <cstring>
     5 using namespace std;
     6 
     7 const int maxn = 200000 + 20;
     8 char s1[maxn], s2[maxn * 2], real;
     9 int p[maxn * 2];
    10 
    11 void init(char s1[], char s2[])
    12 {
    13     int j = 2;
    14     s2[0] = '$', s2[1] = '#';
    15     for(int i = 0; s1[i] != ''; ++i)
    16     {
    17         s2[j++] = s1[i];
    18         s2[j++] = '#';
    19     }
    20     s2[j] = '';
    21 }
    22 
    23 void manacher(char s[])
    24 {
    25     p[0] = 0;
    26     int id, mx = 0;
    27     for(int i = 1; s[i] != ''; ++i)
    28     {
    29         if(mx > i)
    30             p[i] = min(p[id*2-i], mx-i);
    31         else
    32             p[i] = 1;
    33         while(s[i + p[i]] == s[i - p[i]])
    34             ++p[i];
    35         if(i + p[i] > mx)
    36         {
    37             id = i;
    38             mx = i + p[i];
    39         }
    40     }
    41 }
    42 
    43 char change(char c)
    44 {
    45     c -= real - 'a';
    46     if(c < 'a')    c += 26;
    47     if(c > 'z')    c -= 26;
    48     return c;
    49 }
    50 
    51 void getans(void)
    52 {
    53     int mlen = 1, pos;
    54     for(int i = 1; s2[i] != ''; ++i)
    55     {
    56         if(p[i] - 1 > mlen)
    57         {
    58             mlen = p[i] - 1;
    59             pos = i;
    60         }
    61     }
    62     if(mlen == 1)
    63         printf("No solution!
    ");
    64     else
    65     {
    66         int L = (pos - (p[pos] - 2)) / 2 - 1;
    67         int R = (pos + (p[pos] - 2)) / 2 - 1;
    68         printf("%d %d
    ", L, R);
    69         for(int i = L; i <= R; ++i)
    70             printf("%c", change(s1[i]));
    71         printf("
    ");
    72     }
    73 }
    74 
    75 int main(void)
    76 {
    77     #ifdef LOCAL
    78         freopen("3294in.txt", "r", stdin);
    79     #endif
    80 
    81     
    82     while(scanf("%c", &real) == 1)
    83     {
    84         scanf("%s", s1);
    85         getchar();
    86         init(s1, s2);
    87         manacher(s2);
    88         getans();
    89     }
    90     return 0;
    91 }
    代码君
  • 相关阅读:
    shell 函数用法
    shell read变量的读入
    利用系统函数模拟实现nginx 系统脚本启动的特殊颜色专业效果
    shell重定向介绍及使用
    监控MySQL或Web服务是否正常
    centos 6.5下安装nmap工具及简单用法
    if条件简单语法
    shell 的条件表达式及逻辑操作符简单介绍
    mysql常见的错误代码
    Linux MySql 安装与配置(二进制包)
  • 原文地址:https://www.cnblogs.com/AOQNRMGYXLMV/p/3920317.html
Copyright © 2020-2023  润新知