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 pand 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.
Hint
题意为对于给定的一个字符串,判断每进行一次操作(将x位置处的字母变成字符c)后该字符串是否为回文串,即从前往后与
从后往前是一样的,输出一共多少个这样的操作。
#include <iostream>
#include <cstring>
using namespace std;
const int maxn = 1e5+4;
char s[maxn];
int main()
{
int t,i,n,m,sum1,len,sum,x;
char c;
cin>>t;
while(t--)
{
cin>>n>>m;
cin>>s;
sum=0;
len=(n-1)/2;
for(i=0;i<=len;i++)///找有多少个相对位置不相等情况
{
if(s[i]!=s[n-1-i])
{
sum++;
}
}
sum1=0;
for(i=0;i<m;i++)
{
cin>>x>>c;
x--;
if(x==len&&n%2==0)///字符串长度为2的倍数,并且改变的位置为中间位置
{
if(s[x]!=s[x+1])
{
if(c==s[x+1])
{
s[x]=c;
sum--;
if(sum==0)sum1++;
}
else if(c!=s[x+1])s[x]=c;
}
else if(s[x]==s[x+1])
{
if(c!=s[x+1])
{
sum++;
s[x]=c;
}
}
}
else if(x==len&&n%2)///字符串长度为奇数
{
s[x]=c;
if(sum==0)sum1++;
}
else if(x!=len)///当改变的位置不是中间的位置
{
if(s[x]==s[n-1-x])
{
if(c==s[x])
{
if(sum==0)sum1++;
}
else if(c!=s[x])
{
sum++;
s[x]=c;
}
}
else if(s[x]!=s[n-1-x])
{
if(c==s[n-1-x])
{
s[x]=c;
sum--;
if(sum==0)sum1++;
}
else if(c!=s[n-1-x])s[x]=c;
}
}
}
cout<<sum1<<endl;
}
return 0;
}