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.
Input
1
10 7
abcdefdcba
5 x
6 x
4 d
2 d
3 y
8 y
9 d
Output
3
AC代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
long long int t,x,y,i,j,z,m,n;
long long int sum;
char a[100005],c[100005],s;
scanf("%lld",&t);
while(t--)
{
m=0;
sum=0;
scanf("%lld %lld",&x,&y);
scanf("%s",a);
for(z=0; z<x; z++)
c[z]=a[x-z-1];
for(z=0; z<x; z++)
if(c[z]!=a[z])m++;
while(y--)
{
getchar();
scanf("%lld %c",&n,&s);
if(x%2=1&&n=x/2+1)sum=sum;
else
{
if(c[n-1]=s&&a[n-1]!=s)
{
m=m-2;
}
else if(c[n-1]=a[n-1]&&s!=a[n-1])m=m+2;
a[n-1]=s;
c[x-n]=s;
}
if(m==0)sum++;
}
printf("%lld\n",sum);
}
return 0;
}