cf 570 C. Replacement (暴力)
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.
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 andci (1 ≤ xi ≤ n, ci — a lowercas English letter or a period sign), describing the query of assigning symbol ci to position xi.
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.
10 3
.b..bz....
1 h
3 c
9 f
4
3
1
4 4
.cc.
2 .
3 .
2 a
1 a
1
3
1
1
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.")
对于每一个改变,如果是字母变成周期符号或者周期符号变成字母,我们称之为有效改变.
对于每个有效改变,只有当这个位置左边有周期符号的时候,会影响答案.
直接扫一遍即可.复杂度o(n+m)
抱歉之前代码帖错了...我的锅...
/*************************************************************************
> File Name: code/cf/#316/C.cpp
> Author: 111qqz
> Email: rkz2013@126.com
> Created Time: 2015年08月14日 星期五 00时54分50秒
************************************************************************/
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<stack>
#define y0 abc111qqz
#define y1 hust111qqz
#define yn hez111qqz
#define j1 cute111qqz
#define tm crazy111qqz
#define lr dying111qqz
using namespace std;
#define REP(i, n) for (int i=0;i<int(n);++i)
typedef long long LL;
typedef unsigned long long ULL;
const int inf = 0x7fffffff;
const int N=3E5+7;
int n,m;
char st[N];
int judge(char a,char b)
{
if (a>='a'&&a<='z'&&b>='a'&&b<='z') return 0;
if (a=='.'&&b=='.') return 0;
if (a>='a'&&a<='z'&&b=='.') return 1;
if (a=='.'&&b>='a'&&b<='z') return -1;
}
int main()
{
cin>>n>>m;
getchar();
st[0]=' ';
for ( int i = 1 ; i <= n ; i++)
{
scanf("%c",&st[i]);
}
int cnt = 0;
for ( int i = 1 ;i <= n - 1 ; i++)
{
if (st[i]=='.'&&st[i+1]=='.')
{
cnt++;
}
}
// cout<<"cnt :"<<cnt<<endl;
int x;
char ch;
for ( int i = 1 ; i <= m ; i++)
{
scanf("%d",&x);
getchar();
scanf("%c",&ch);
int flag =judge(st[x],ch);
st[x] = ch;
if (flag==0)
{
cout<<cnt<<endl;
continue;
}
if (flag==1)
{
if (st[x-1]=='.') cnt++;
if (st[x+1]=='.') cnt++;
cout<<cnt<<endl;
continue;
}
if (flag==-1)
{
if (st[x-1]=='.') cnt--;
if (st[x+1]=='.') cnt--;
cout<<cnt<<endl;
continue;
}
}
return 0;
}