zoukankan      html  css  js  c++  java
  • HDU-3294-Girls' research(Manacher)

    链接:

    http://acm.hdu.edu.cn/showproblem.php?pid=3294

    题意:

    One day, sailormoon girls are so delighted that they intend to research about palindromic strings. Operation contains two steps:
    First step: girls will write a long string (only contains lower case) on the paper. For example, "abcde", but 'a' inside is not the real 'a', that means if we define the 'b' is the real 'a', then we can infer that 'c' is the real 'b', 'd' is the real 'c' ……, 'a' is the real 'z'. According to this, string "abcde" changes to "bcdef".
    Second step: girls will find out the longest palindromic string in the given string, the length of palindromic string must be equal or more than 2.

    思路:

    字符串的转换就是对每个字符, 偏移c-'a', c为给的字符,
    然后跑马拉车, 算出在原串上的左端点,即可.

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <vector>
    //#include <memory.h>
    #include <queue>
    #include <set>
    #include <map>
    #include <algorithm>
    #include <math.h>
    #include <stack>
    #include <string>
    #include <assert.h>
    #include <iomanip>
    #include <iostream>
    #include <sstream>
    #define MINF 0x3f3f3f3f
    using namespace std;
    typedef long long LL;
    const int MAXN = 4e5+10;
    
    char a[MAXN], b[MAXN];
    int Hw[MAXN];
    int n;
    
    void Manacher(char *ori)
    {
        int len = strlen(ori);
        int maxr = 0, mid;
        for (int i = 1;i < len;i++)
        {
            if (i < maxr)
                Hw[i] = min(Hw[2*mid-i], maxr-i);
            else
                Hw[i] = 1;
            while (ori[i+Hw[i]] == ori[i-Hw[i]])
                Hw[i]++;
            if (i+Hw[i] < maxr)
            {
                maxr = i+Hw[i];
                mid = i;
            }
        }
    }
    
    void Change(char *ori, char *pet)
    {
        int len = strlen(ori);
        pet[0] = pet[1] = '#';
        for (int i = 0;i < len;i++)
        {
            pet[2*i+2] = ori[i];
            pet[2*i+3] = '#';
        }
        pet[2*len+2] = 0;
    }
    
    int main()
    {
        char c;
        while (~scanf("%c %s", &c, a))
        {
            getchar();
            int l = c-'a';
            int len = strlen(a);
            for (int i = 0;i < len;i++)
                a[i] = (a[i]-'a'-l+26)%26+'a';
            Change(a, b);
            Manacher(b);
            int p = 0;
            len = strlen(b);
            for (int i = 0;i < len;i++)
            {
                if (Hw[i] > Hw[p])
                    p = i;
            }
            if (Hw[p]-1 < 2)
                puts("No solution!");
            else
            {
                int left = (p-Hw[p])/2;
                int right = left+Hw[p]-2;
                a[right+1] = 0;
                printf("%d %d
    ", left, right);
                printf("%s
    ", a+left);
            }
        }
    
        return 0;
    }
    
  • 相关阅读:
    jenkins 分布式部署
    Jenkins2.138配置slave节点时,启动方法只有两个选项
    SIFT特征详解
    OpenCV,计算两幅图像的单应矩阵
    OpenGL新手框架
    OpenGL超级宝典visual studio 2013开发环境配置,GLTools
    归一化变换 Normalizing transformations
    OpenCV2:特征匹配及其优化
    OpenCV2简单的特征匹配
    Qt自适应大小显示图片,添加菜单
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11606288.html
Copyright © 2011-2022 走看看