zoukankan      html  css  js  c++  java
  • codeforces570C

    Replacement

     CodeForces - 570C 

    话说很久很久以前,孙悟空被压在了山下,很无聊。于是他找了一个只包含小写字母和字符"." 的字符串。 由于他比较无聊,他就开始了换点点,他找到字符串中第一个出现的"..",然后把其换成"."。 如果字符串中不包含"..",换点点操作对字符串无效

    现在定义一个值f(s),表示使字符串不包含".."的最小 换点点 操作次数。

    你现在需要处理m个询问, 第i个询问时,你需要把字符串中的 xi (1 ≤ xi ≤ n) 位置的字符替换成 ci。替换后,输出 f(s)的值。

    这下孙悟空一脸懵逼,快来帮帮他~

    Input

    第一行包括两个整数 n 和 m (1 ≤ n, m ≤ 300 000) ,分别表示字符串长度和询问次数。

    第二行包含一个长度为n的字符串,字符串只包含小写字母和字符"."

    接下来m行描述询问。第i行包括 xi 和 ci (1 ≤ xi ≤ nci — 一个小写字母或一个字符".")

    Output

    输出m个数字,每个一行 ,输出执行第i个替换后的f(s)值。

    Example

    Input
    10 3
    .b..bz....
    1 h
    3 c
    9 f
    Output
    4
    3
    1
    Input
    4 4
    .cc.
    2 .
    3 .
    2 a
    1 a
    Output
    1
    3
    1
    1

    Note

    第一个样例

    初始串:".b..bz....".

    • 第一次询问: f(hb..bz....) = 4    ("hb[..]bz...."  →  "hb.bz[..].."  → "hb.bz[..]."  →  "hb.bz[..]"  →  "hb.bz.")
    • 第二次询问: f(hbс.bz....) = 3    ("hbс.bz[..].."  →  "hbс.bz[..]."  → "hbс.bz[..]"  →  "hbс.bz.")
    • 第三次询问: f(hbс.bz..f.) = 1    ("hbс.bz[..]f."  →  "hbс.bz.f.")

    第二个样例

    初始串:".cc.".

    • 第一次询问: f(..c.) = 1    ("[..]c."  →  ".c.")
    • 第二次询问: f(....) = 3    ("[..].."  →  "[..]."  →  "[..]"  →  ".")
    • 第三次询问:f(.a..) = 1    (".a[..]"  →  ".a.")
    • 第四次询问:f(aa..) = 1    ("aa[..]"  →  "aa.")

    sol:有加强版的是区间修改(我一眼秒了)--摘自某大佬原话

    我太菜了,于是只会做弱化弱化弱化弱化版

    首先很显然的东西,合并的次数= '.' 的个数和连续的 '.' 的段数。所以我们只要维护那两个东西

    因为是单点修改,大力枚举四种情况即可

    Ps:码力太弱,有些吃力

    #include <bits/stdc++.h>
    using namespace std;
    typedef int ll;
    inline ll read()
    {
        ll s=0;
        bool f=0;
        char ch=' ';
        while(!isdigit(ch))
        {
            f|=(ch=='-'); ch=getchar();
        }
        while(isdigit(ch))
        {
            s=(s<<3)+(s<<1)+(ch^48); ch=getchar();
        }
        return (f)?(-s):(s);
    }
    #define R(x) x=read()
    inline void write(ll x)
    {
        if(x<0)
        {
            putchar('-'); x=-x;
        }
        if(x<10)
        {
            putchar(x+'0');    return;
        }
        write(x/10);
        putchar((x%10)+'0');
        return;
    }
    #define W(x) write(x),putchar(' ')
    #define Wl(x) write(x),putchar('
    ')
    const int N=300005;
    int n,m;
    char S[N];
    int main()
    {
        int i,j,cnt=0,Duans=0;
        R(n); R(m);
        scanf("%s",S+1);
        for(i=1;i<=n;i++) if(S[i]=='.') cnt++;
        for(i=1;i<=n;i++) if(S[i]=='.')
        {
            for(j=i;j<=n&&(S[j]=='.');j++);
            Duans++;
            i=j;
        }
        while(m--)
        {
            int Pos=read();
            char SS[5],ch;
            scanf("%s",SS+1); ch=SS[1];
            if(((S[Pos]=='.')||(ch=='.'))&&(S[Pos]!=ch))
            {
                if(S[Pos]=='.')
                {
                    cnt--;
                    if(Pos>1&&S[Pos-1]=='.'&&Pos<n&&S[Pos+1]=='.') Duans++;
                    if((Pos==1||S[Pos-1]!='.')&&(Pos==n||S[Pos+1]!='.')) Duans--;
                }
                else
                {
                    cnt++;
                    if(Pos>1&&S[Pos-1]=='.'&&Pos<n&&S[Pos+1]=='.') Duans--;
                    if((Pos==1||S[Pos-1]!='.')&&(Pos==n||S[Pos+1]!='.')) Duans++;
                }
            }
            S[Pos]=ch;
    //        printf("cnt=%d Duans=%d
    ",cnt,Duans);
            Wl(cnt-Duans);
        }
        return 0;
    }
    /*
    input
    10 3
    .b..bz....
    1 h
    3 c
    9 f
    output
    4
    3
    1
    
    input
    4 4
    .cc.
    2 .
    3 .
    2 a
    1 a
    output
    1
    3
    1
    1
    */
    View Code
  • 相关阅读:
    codeforces 814B An express train to reveries
    codeforces 814A An abandoned sentiment from past
    codeforces 785D D. Anton and School
    codeforces 785C Anton and Fairy Tale
    codeforces 791C Bear and Different Names
    AOP详解
    Spring集成JUnit测试
    Spring整合web开发
    IOC装配Bean(注解方式)
    IOC装配Bean(XML方式)
  • 原文地址:https://www.cnblogs.com/gaojunonly1/p/10651316.html
Copyright © 2011-2022 走看看