Genta Game Gym - 101778H

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 m lines 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
1
10 7
abcdefdcba
5 x
6 x
4 d
2 d
3 y
8 y
9 d
Output
3
Note
A 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 2rd, 3th, and 7th operations.
思路:
直接暴力每次判断回文串肯定会超时,所以可以转变思路,由于题目中每次仅改变一个字母,可以先统计字符串中对称不是回文串的字母有哪些,然后根据操作判断此操作是否影响回文串的判断,即减少不同点还是增加不同点。
详见代码


```cpp

```cpp
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
char s[N];
int main()
{
    int n,m;
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d %d",&n,&m);
        getchar();
        gets(s);
        int dif=0;//记录起始串的不同点
        for(int i=0,j=n-1;j>i;i++,j--){
            if(s[i]!=s[j]) dif++;
        }
        int cnt=0;
        while(m--){
            char nc;
            int x;
            scanf("%d %c",&x,&nc);
            char c=s[x-1];
            s[x-1]=nc;
            if(s[x-1]==s[n-x]&&c!=s[n-x]&&(x-1)!=(n-x)){//现在相同 之前的不相同 以及不是自身
                dif--;
            }
            else if(s[x-1]!=s[n-x]&&c==s[n-x]){//现在新的不相同 之前是相同的
                dif++;
            }
            if(dif==0) cnt++;
        }
        printf("%d\n",cnt);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值