cf 570 C. Replacement (暴力)
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 x__i (1 ≤ x__i ≤ n) of string s is assigned value c__i. 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 x__i and_c__i_ (1 ≤ x__i ≤ n, c__i -- a lowercas English letter or a period sign), describing the query of assigning symbol c__i to position x__i.
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.
Sample test(s)
input
10 3<br></br>.b..bz....<br></br>1 h<br></br>3 c<br></br>9 f
output
4<br></br>3<br></br>1
input
4 4<br></br>.cc.<br></br>2 .<br></br>3 .<br></br>2 a<br></br>1 a
output
1<br></br>3<br></br>1<br></br>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.")
对于每一个改变,如果是字母变成周期符号或者周期符号变成字母,我们称之为有效改变.
对于每个有效改变,只有当这个位置左边有周期符号的时候,会影响答案.
直接扫一遍即可.复杂度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
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#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;itypedef 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 :"< 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< continue;
}
if (flag==1)
{
if (st[x-1]=='.') cnt++;
if (st[x+1]=='.') cnt++;
cout< continue;
}
if (flag==-1)
{
if (st[x-1]=='.') cnt--;
if (st[x+1]=='.') cnt--;
cout< continue;
}
}
return 0;
}