SDUT 2021 Spring Individual Contest(for 20) - 12 补题

H题Genta Game
题意:首先给定n和m,n为字符串长度,接着给定字符串,m次操作,问m次操作中有多少次会出现回文串
思路:因为给的数据范围是1e5,所以时间复杂度应该控制在O(nlogn)以内。首先先把给定的字符串预处理一下,查询字符串中有多少个不回文的部分,之后的每一次操作中,操作结束时判断不回文的部分是否为0,如果是,结果加一。

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
char a[N];
int main()
{
    
    
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
    
    
        int res=0;
        int ans=0;
        int n,m;
        cin>>n>>m;
        cin>>a;
        int len=strlen(a);
        for(int i=0,j=len-1;i<=j;i++,j--)
        {
    
    
            if(a[i]!=a[j])
            res++;
        }   
        for(int i=0;i<m;i++)
        {
    
    
            int x;
            char c;
            cin>>x>>c;
            if(n%2==1&&x==n/2+1&&res==0)//特殊情况判定
            ans++;
            x--;
            if(a[x]!=a[n-1-x])
            {
    
    
                if(a[n-1-x]==c)
                {
    
    
                    res--;
                }
            }
            else
            {
    
    
                if(a[x]!=c)
                {
    
    
                    res++;
                }
            }
            a[x]=c;
            if(res==0)
            ans++;
        }
        cout<<ans<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_51768569/article/details/115419508