• Codeforces Round #316 (Div. 2) C 思路/模拟


    C. Replacement
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Daniel has a string s, consisting of lowercase English letters and period signs (characters '.'). Let's define the operation of replacement as the following sequence of steps: find a substring ".." (two consecutive periods) in string s, of all occurrences of the substring let's choose the first one, and replace this substring with string ".". In other words, during the replacement operation, the first two consecutive periods are replaced by one. If string s contains no two consecutive periods, then nothing happens.

    Let's define f(s) as the minimum number of operations of replacement to perform, so that the string does not have any two consecutive periods left.

    You need to process m queries, the i-th results in that the character at position xi (1 ≤ xi ≤ n) of string s is assigned value ci. After each operation you have to calculate and output the value of f(s).

    Help Daniel to process all queries.

    Input

    The first line contains two integers n and m (1 ≤ n, m ≤ 300 000) the length of the string and the number of queries.

    The second line contains string s, consisting of n lowercase English letters and period signs.

    The following m lines contain the descriptions of queries. The i-th line contains integer xi and ci (1 ≤ xi ≤ n, ci — a lowercas English letter or a period sign), describing the query of assigning symbol ci to position xi.

    Output

    Print m numbers, one per line, the i-th of these numbers must be equal to the value of f(s) after performing the i-th assignment.

    Examples
    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

    Note to the first sample test (replaced periods are enclosed in square brackets).

    The original string is ".b..bz....".

    • after the first query f(hb..bz....) = 4    ("hb[..]bz...."  →  "hb.bz[..].."  →  "hb.bz[..]."  →  "hb.bz[..]"  →  "hb.bz.")
    • after the second query f(hbс.bz....) = 3    ("hbс.bz[..].."  →  "hbс.bz[..]."  →  "hbс.bz[..]"  →  "hbс.bz.")
    • after the third query f(hbс.bz..f.) = 1    ("hbс.bz[..]f."  →  "hbс.bz.f.")

    Note to the second sample test.

    The original string is ".cc.".

    • after the first query: f(..c.) = 1    ("[..]c."  →  ".c.")
    • after the second query: f(....) = 3    ("[..].."  →  "[..]."  →  "[..]"  →  ".")
    • after the third query: f(.a..) = 1    (".a[..]"  →  ".a.")
    • after the fourth query: f(aa..) = 1    ("aa[..]"  →  "aa.")

    题意:给你长度为n的字符串  m组替换和查询xi and ci   查询 替换之后字符串中有多少个[..]

    题解: 先统计一下初始的字符串有多少组[..]

            然后考虑每次替换对[..]有什么影响呢 ?若ci为‘.’ 才有可能增加答案 若为其他字符才有可能减少答案  

            并且只能与 xi + 1,xi - 1两个位置产生影响  最多增加或减少两个[..]

            细细分析每种情况。注意边界判断/注意替换前x位置是什么字符。 具体看代码。

            

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<queue>
     5 #include<stack>
     6 #include<vector>
     7 #include<map>
     8 #include<algorithm>
     9 #define ll __int64
    10 #define mod 1e9+7
    11 #define PI acos(-1.0)
    12 using namespace std;
    13 int n,m;
    14 char a[300005];
    15 int main()
    16 {
    17     scanf("%d %d",&n,&m);
    18     getchar();
    19     scanf("%c",&a[1]);
    20     int ans=0;
    21     for(int i=2;i<=n;i++)
    22     {
    23         scanf("%c",&a[i]);
    24         if(a[i]=='.'&&a[i-1]=='.')
    25             ans++;
    26     }
    27     int  pos;
    28     char exm;
    29     for(int i=1;i<=m;i++)
    30     {
    31         scanf("%d %c",&pos,&exm);
    32         if(exm=='.')
    33         {
    34             if(pos-1>=1&&a[pos]!='.')
    35             {
    36                 if(a[pos-1]=='.')
    37                     ans++;
    38             }
    39             if(pos+1<=n&&a[pos]!='.')
    40             {
    41                 if(a[pos+1]=='.')
    42                     ans++;
    43             }
    44             a[pos]=exm;
    45         }
    46         else
    47         {
    48            if(pos-1>=1&&a[pos]=='.')
    49             {
    50                 if(a[pos-1]=='.')
    51                     ans--;
    52             }
    53             if(pos+1<=n&&a[pos]=='.')
    54             {
    55                 if(a[pos+1]=='.')
    56                     ans--;
    57             }
    58             a[pos]=exm;
    59         }
    60         cout<<ans<<endl;
    61     }
    62     return 0;
    63 }
  • 相关阅读:
    (1)MySQL初识
    ESP8266(WIFI模组)_Lua开发:环境搭建
    那些年写过的毕设论文
    单片机的心跳SysTick
    尝试
    菜单管理设计说明
    mybatis的mapper.xml中sql的用法
    powerdesigner
    Cookie
    java自定义注解
  • 原文地址:https://www.cnblogs.com/hsd-/p/5685397.html
Copyright © 2020-2023  润新知