一个容易TL的题目



题目:
Description
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.

Sample Input
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
Hint
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表示字符串的长度和查询次数,然后输入该字符串,每次都得根据要求进行查询,然后必须把每两个相邻的'.'进行合并,知道不再有相邻的'.’出现,最后算出得到最简字符串后所需要的步数并输出。

思路:这个题目我一开始做了好几次都时间超限,当时我一直很郁闷为什么会超限,改哪里可以减少时间,后面是看了大神的代码我才恍然大悟,在做这种类似的题目时,我们可以在一开始就算出得到最简字符串需要的步数,在按照每次的查询去进行判断,并按要求增减步数,这样就不需要在每查询一次时都要把整个字符串从头到尾检查一次,大大提高了效率。我会把我TL(时间超限)的代码和A过的代码贴出来,大家可以比较一下。

TL代码如下:

#include<iostream>  //时间超限
#include<string>
using namespace std;
int main()
{
	string str,str1;
	int n,m,a,i,sum;
	char s;
	while(cin>>n>>m)
	{
		cin>>str;
		while(m--)
		{
			sum=0;
			cin>>a>>s;
			str[a-1]=s;
			str1=str;
			for(i=1;i<str.size();i++)
			{
				if(str[i]=='.' &&str[i-1]=='.')
				{
					sum++;
					str.erase(i,1);
					i--;
				}
			}
			str=str1;
			cout<<sum<<endl;
		}
	}
	return 0;
}

#include<iostream>  
#include<string>
using namespace std;
int main()
{
	string str,str1;
	int n,m,a,sum,b;
	char s;
	while(cin>>n>>m)
	{
		cin>>str;
		while(m--)
		{
			sum=0;
			cin>>a>>s;
			str[a-1]=s;
			b=str.find('.',0);
			while(b<str.size())
			{
				if(str[b+1]=='.')
					sum++;
				b=str.find('.',b+1);
			}
			cout<<sum<<endl;
		}
	}
	return 0;
}


正确代码(A):

#include<iostream>
#include<string>
using namespace std;
int main()
{
	int n,m,i,k;
	string str;
	while(cin>>n>>m>>str)
	{
		k=0;
		for(i=1;i<str.length();i++)
		{
			if(str[i]=='.'&&str[i-1]=='.')
				k++;
		}
		while(m--)
		{
			int a,j=0;
			char c;
			cin>>a>>c;
			if(c=='.'&& str[a-1]!='.')
			{
				if(a-2>=0)
				{ 
					 if(str[a-2]=='.') k++;
				}
				if(a<str.size())
				{
				   if(str[a]=='.') k++;
				}
			}
			if(c!='.'&& str[a-1]=='.')
			{
				
				if(a-2>=0)
				{ 
					 if(str[a-2]=='.') k--;
				}
				if(a<str.size())
				{
				   if(str[a]=='.') k--;
				}
			}
			str[a-1]=c;
			cout<<k<<endl;
		}
	}
	return 0;
}




版权声明:本文为hj1214ing原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。