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;
    }
    
  • 相关阅读:
    mysql GROUP_CONCAT 查询某个字段(查询结果默认逗号拼接)
    mysql中find_in_set的使用
    Libev源码分析07:Linux下的eventfd简介
    Libev源码分析06:异步信号同步化--sigwait、sigwaitinfo、sigtimedwait和signalfd
    Nova中的Hook机制
    Python深入:stevedore简介
    Libev源码分析05:Libev中的绝对时间定时器
    Python深入:setuptools简介
    Libev源码分析04:Libev中的相对时间定时器
    Libev源码分析02:Libev中的IO监视器
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11606288.html
Copyright © 2011-2022 走看看