zoukankan      html  css  js  c++  java
  • Codeforces Gym 100570 E. Palindrome Query Manacher

    E. Palindrome Query
    Time Limit: 20 Sec

    Memory Limit: 256 MB

    题目连接

    http://codeforces.com/gym/100570/problem/E

    Description

    De Prezer loves palindrome strings. A string s1s2...sn is palindrome if and only if it is equal to its reverse.

    De Prezer also loves queries.

    You are given string s of length n and m queries. There are 3 types of queries :

    1. p x : Modify sp = x where 1 ≤ p ≤ n and x is a lower case English letter.

    2. p : Print the length of the largest palindrome substring of s like slsl + 1...sr such that l ≤ p ≤ r and r - p = p - l. (1 ≤ p ≤ n)

    3. p : Print the length of the largest palindrome substring of s like slsl + 1...sr such that l ≤ p and p + 1 ≤ r and r - p - 1 = p - l. (1 ≤ p ≤ n - 1) or  - 1 if there is no such substring.

    Input

    The first line of input contains s and m.

    Next m lines contain queries.

    1 ≤ n, m ≤ 105

    s only contains lower case English letters.

    Output

    For each query of type 2 and 3 print the answer in a single line.

    Sample Input

    abcd 3
    3 1
    1 2 c
    3 2

    Sample Output

    -1
    2

    HINT

    题意

    给你一个字符串,然后有3个操作

    1.修改一个位置的字符为

    2.查询以p为中心的奇数回文串最长长度

    3.查询以p为中心的偶数回文串最长长度

    题解

    这道题看起来很唬人,其实暴力可过……

    直接傻逼暴力就好

    我拍的是manacher,直接暴力修改,也是直接过了……

    代码

    //qscqesze
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    #include <map>
    #include <stack>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define test freopen("test.txt","r",stdin)
    #define maxn 201001
    #define mod 10007
    #define eps 1e-9
    int Num;
    char CH[20];
    //const int inf=0x7fffffff;   //нчоч╢С
    const int inf=0x3f3f3f3f;
    /*
    
    inline void P(int x)
    {
        Num=0;if(!x){putchar('0');puts("");return;}
        while(x>0)CH[++Num]=x%10,x/=10;
        while(Num)putchar(CH[Num--]+48);
        puts("");
    }
    */
    inline ll read()
    {
        ll x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    inline void P(int x)
    {
        Num=0;if(!x){putchar('0');puts("");return;}
        while(x>0)CH[++Num]=x%10,x/=10;
        while(Num)putchar(CH[Num--]+48);
        puts("");
    }
    //**************************************************************************************
    char s[maxn];
    char str[maxn];
    int p[maxn];
    int dp1[maxn];
    int dp2[maxn];
    int l;
    void manacher(char s[],int l)
    {
       int i,j,k,ans=0;
       for(i=1;i<=l;++i)str[i<<1]=s[i],str[(i<<1)+1]='#';
       str[1]='#';str[l*2+1]='#';str[0]='&';str[l*2+2]='$';
       l=l*2+1;j=0;
       for(i=1;i<=l;)
       {
           while(str[i-j-1]==str[i+j+1])++j;
           p[i]=j;if(j>ans)ans=j;
           for(k=1;k<=j&&p[i]-k!=p[i-k];++k)p[i+k]=min(p[i-k],p[i]-k);
           i+=k;j=max(j-k,0);
       }
    }
    struct node
    {
        int p;
        char c;
    };
    vector<node> Q;
    int main()
    {
        //test;
        int m;
        scanf("%s",s+1);
        scanf("%d",&m);
        l=strlen(s+1);
        manacher(s,l);
        l=l*2+1;
        for(int ii=0;ii<m;ii++)
        {
            int k;
            scanf("%d",&k);
            if(k==1)
            {
                int pp;
                pp=read();
                char c;
                scanf("%c",&c);
                Q.push_back((node){pp,c});
            }
            if(k==2)
            {
                if(Q.size())
                {
                    for(int i=0;i<Q.size();i++)
                    {
                        s[Q[i].p]=Q[i].c;
                    }
                    Q.clear();
                    manacher(s,l/2);
                }
                int pp=read();
                printf("%d
    ",p[pp*2]);
            }
            if(k==3)
            {
                if(Q.size())
                {
                    for(int i=0;i<Q.size();i++)
                    {
                        s[Q[i].p]=Q[i].c;
                    }
                    Q.clear();
                    manacher(s,l/2);
                }
                int pp=read();
                if(p[pp*2+1]==0)
                    printf("-1
    ");
                else
                    printf("%d
    ",p[pp*2+1]);
            }
        }
    }
  • 相关阅读:
    黑松白鹿
    跨越
    第三年
    Lua windows环境搭建
    Iron man
    水果沙拉
    六周岁
    sqlserver数据库附加报错5120
    [BeiJing2006]狼抓兔子 平面图最小割
    BZOJ2118: 墨墨的等式 思维建图
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4684565.html
Copyright © 2011-2022 走看看