ZOJStrings in the Pocket 16th浙江省省赛

版权声明:点个赞再走呗,转载请注明出处 https://blog.csdn.net/qq_41431457/article/details/89643255

Strings in the Pocket


Time Limit: 1 Second      Memory Limit: 65536 KB


BaoBao has just found two strings and in his left pocket, where indicates the -th character in string , and indicates the -th character in string .

As BaoBao is bored, he decides to select a substring of and reverse it. Formally speaking, he can select two integers and such that and change the string to .

In how many ways can BaoBao change to using the above operation exactly once? Let be an operation which reverses the substring , and be an operation which reverses the substring . These two operations are considered different, if or .

Input

There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:

The first line contains a string ( ), while the second line contains another string ( ). Both strings are composed of lower-cased English letters.

It's guaranteed that the sum of of all test cases will not exceed .

Output

For each test case output one line containing one integer, indicating the answer.

Sample Input

2
abcbcdcbd
abcdcbcbd
abc
abc

Sample Output

3
3

Hint

For the first sample test case, BaoBao can do one of the following three operations: (2, 8), (3, 7) or (4, 6).

For the second sample test case, BaoBao can do one of the following three operations: (1, 1), (2, 2) or (3, 3).

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4110

主要难点就是求回文串的个数(马拉车算法)

manachar algorithm: https://blog.csdn.net/qq_41431457/article/details/88355807

如果两个字符串相等 求回文串的数量

否则 纯模拟

#include<bits/stdc++.h>
#define lom long long
#define M 2000005
using namespace std;
int a[M],b[M];
string sa,sb;
lom Ipss(string s)//to get number of palindromes(manachar algorithm)
{
	lom ans=0;
	int lo,ro,le,re,xo,xe,len=s.length();
	le=lo=xe=xo=0;	re=ro=-1;
	for(int i=0;i<len;i++)
	{
		if(i>ro) xo=1;
		else xo=min(ro-i,a[lo+ro-i]);
		
		while(i-xo>=0&&i+xo<len&&s[i-xo]==s[i+xo]) xo++;
		
		a[i]=xo;	ans+=xo;
		
		if(i+xo-1>ro) lo=i-xo+1, ro=i+xo-1;
		
	//--------------------------------------------------	
		if(i>re) xe=0;
		else xe=min(b[le+re-i+1],re-i+1);
		
		while(i-xe-1>=0&&i+xe<len&&s[i-xe-1]==s[i+xe]) xe++;
		
		b[i]=xe; ans+=xe;
		
		if(i+xe>=re) le=i-xe, re=i+xe-1;
	}
	
	return ans;
}
int main()
{
	ios::sync_with_stdio(false);
	int t;
	cin>>t;
	while(t--)
	{
		cin>>sa>>sb;
		int len=sa.length(),i=0,j=len-1,flag=0,sum=0;
		
		while(sa[i]==sb[i]&&i<len) i++;//start from begin to confirm where is not equal
		if(i==len)//if two strings completely equals 
		{
			cout<<Ipss(sb)<<endl;//get sum of palindromes substring 
			continue;
		}
		while(sa[j]==sb[j]&&j>=0) j--;//start from end
		
		//Determine whether the two strings are equal after flipping
		for(int k1=i,k2=j;k1<=j&&k2>=i;k1++,k2--)
		if(sa[k1]!=sb[k2])//if note equals 
		{
			flag=1;
			cout<<'0'<<endl;
			break;
		}
		if(flag==1) continue;
		
		//sqread firom on both side
		for(int k1=i-1,k2=j+1; k1>=0&&k2<len&&sa[k1]==sa[k2]; k1--,k2++) sum++;
		cout<<sum+1<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41431457/article/details/89643255