Gym - 101778H Genta Game(暴力模拟)

H. Genta Game
time limit per test
1.0 s
memory limit per test
256 MB
input
standard input
output
standard output

Kojima Genta is one of the best friends of Conan, and the fattest one!

Everyone believes that Genta is just thinking about food. So, he wants to prove the opposite. So, his friends challenged him in a game. Genta's friends will give him a string s of length n, and m update operations. At each update operation, an integer p (1 ≤ p ≤ n) and a lowercase English letter c will be given to Genta, and he is asked to change the pth letter in the string s to the letter c.

Conan explained to Genta that an update operation is said to be beautiful if the string s was a palindrome string after the update operation has been executed.

Genta task is to count the number of beautiful update operations. Genta wants to win in this game no matter what this will cost because his friends promised him that the food will be at their expense throughout the week if he solved the task. Can you help Genta by solving his task?

Input

The first line contains an integer T (1 ≤ T ≤ 50), in which T is the number of test cases.

The first line of each test cases contains two integers n and m (1 ≤ n, m ≤ 105), in which n is the length of the string s and m is the number of update operations. The second line of each test cases contains a string s of length n consisting of lowercase English letters only. Then mlines follow, each line contains an integer p and a lowercase English letter c (1 ≤ p ≤ n), giving the update operations.

The sum of n and m overall test cases does not exceed 7 × 105 for each.

Output

For each test case, print a single line containing the number of beautiful update operations.

Example
input
Copy
1
10 7
abcdefdcba
5 x
6 x
4 d
2 d
3 y
8 y
9 d
output
Copy
3
Note

palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward, such as "madam" or "racecar".

In the first test case, the string s will be updated as follow:

abcdefdcba  abcdxfdcba  abcdxxdcba  abcdxxdcba  adcdxxdcba  adydxxdcba  adydxxdyba adydxxdyda.

There are 3 beautiful update operations, which are the 2rd3th, and 7th operations.

题意:给你一个字符串长度为n,接下来m次操作,

每次操作的格式为 x ch

意思是把字符串中位置x的字符变为字符ch

如果某次操作后串变成了回文串,则称这次操作是beautiful的。求有多少次beautiful操作。

只需要记录原始串中不回文的位置和个数,然后对每次操作判断即可。。。注意奇数串中间的位置需要特判

这道题应该是很水的题啊,比赛的时候应该读完题就出来了啊。。。为啥比赛的时候第一个做出来的是两个半小时。。。而且全场没有一个1A的?

比赛没参加,赛后随手一敲就过了啊。。。

代码:

#pragma comment(linker,"/STACK:102400000,102400000")
#include<bits/stdc++.h>
#define maxn 200010
#define ll long long
#define inf 1e9+7
using namespace std;
const long long mod=1e9+7;
int n,m,q,k,flag,x,f,y,p;
int ans,tmp,cnt;
int a[maxn],c[maxn];
char s[maxn];
int main()
{
    int T,cas;
    scanf("%d",&T);
    {
        while(T--){
        scanf("%d %d",&n,&m);
        scanf("%s",s+1);
        tmp=0;
        ans=0;
        for(int i=1;i<=n/2;i++)
        {
            if(s[i]==s[n-i+1])
            {
                c[i]=c[n-i+1]=1;
            }
            else
            {
                tmp++;
                c[i]=c[n-i+1]=0;
            }
        }
        int t=0;
        if(n&1) c[(n+1)/2]=1;
        while(m--)
        {
            char ch[2];
            int x;
            scanf("%d %s",&x,ch);
            if((n&1)&&(x==(n+1)/2))
            {
                c[x]=1;
            }
            else if(c[x]==0)
            {
                s[x]=ch[0];
                if(s[x]==s[n-x+1])
                {
                    c[x]=c[n-x+1]=1;
                    tmp--;
                }
            }
            else
            {
               s[x]=ch[0];
               if(s[x]!=s[n-x+1])
               {
                    c[x]=c[n-x+1]=0;
                    tmp++;
               }
            }
            if(tmp==0) ans++;
        }
        printf("%d\n",ans);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/lsd20164388/article/details/80315467