Didi is a curious baby. One day, she finds a curious game, which named Rock Paper Scissors Lizard Spock.
The game is an upgraded version of the game named Rock, Paper, Scissors. Each player chooses an option . And then those players show their choices that was previously hidden at the same time. If the winner defeats the others, she gets a point.
The rules are as follows.
Scissors cuts Paper
Paper covers Rock
Rock crushes Lizard
Lizard poisons Spock
Spock smashes Scissors
Scissors decapitates Lizard
Lizard eats Paper
Paper disproves Spock
Spock vaporizes Rock
(and as it always has) Rock crushes Scissors.
(this pic is from baike.baidu.com)
But Didi is a little silly, she always loses the game. In order to keep her calm, her friend Tangtang writes down the order on a list and show it to her. Didi also writes down her order on another list, like .
(Rock-R Paper-P Scissors-S Lizard-L Spock-K)
However, Didi may skip some her friends' choices to find the position to get the most winning points of the game, like
Can you help Didi find the max points she can get?
Input:
The first line contains the list of choices of Didi's friend, the second line contains the list of choices of Didi.
(1<=len(s2)<=len(s1)<=1e6)
Output:
One line contains an integer indicating the maximum number of wining point.
忽略每行输出的末尾多余空格
RRRRRRRRRLLL RRRS
3
RSSPKKLLRKPS RSRS
2
细节方面,根据相乘结果的含义,对于第一个人的A串,得进行加长,前后分别增加len2-1个长度。这个自己理解一下就知道了。具体见代码:
#include
#define N 2000005
#define PI acos(-1.0)
using namespace std;
map mp;
char s[N],ss[N];
namespace FFT
{
struct Complex
{
double r,i;
Complex(double real=0.0,double image=0.0)
{
r=real; i=image;
}
Complex operator + (const Complex o)
{
return Complex(r+o.r,i+o.i);
}
Complex operator - (const Complex o)
{
return Complex(r-o.r,i-o.i);
}
Complex operator * (const Complex o)
{
return Complex(r*o.r-i*o.i,r*o.i+i*o.r);
}
};
void brc(Complex *y, int l)
{
register int i,j,k;
for( i = 1, j = l / 2; i < l - 1; i++)
{
if (i < j) swap(y[i], y[j]);
k = l / 2; while ( j >= k) j -= k,k /= 2;
if (j < k) j += k;
}
}
void FFT(Complex *y, int len, double on)
{
register int h, i, j, k;
Complex u, t; brc(y, len);
for(h = 2; h <= len; h <<= 1)
{
Complex wn(cos(on * 2 * PI / h), sin(on * 2 * PI / h));
for(j = 0; j < len; j += h)
{
Complex w(1, 0);
for(k = j; k < j + h / 2; k++)
{
u = y[k]; t = w * y[k + h / 2];
y[k] = u + t; y[k + h / 2] = u - t;
w = w * wn;
}
}
}
if (on<0) for (int i = 0; i < len; i++) y[i].r/=len;
}
void multiply(Complex *A,int lenA,Complex *B,int lenB)
{
int len;
for(len = 1; len < lenA + lenB - 1; len <<= 1);
for (int i = lenA; i < len; i++) A[i] = 0;
for (int i = lenB; i < len; i++) B[i] = 0;
FFT(A,len ,1 ); FFT(B, len, 1);
for (int i = 0;i < len; i++) A[i] = A[i] * B[i];
FFT(A, len, -1);
}
}
FFT::Complex A[N],B[N],C[5][N];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
mp['S']=0; mp['P']=1;
mp['R']=2; mp['L']=3; mp['K']=4;
while(cin>>s)
{
cin>>ss;
int len1=strlen(s);
int len2=strlen(ss);
for(int i=0;i<5;i++)
{
using namespace FFT;
memset(A,0,sizeof(A));
memset(B,0,sizeof(B));
for(int j=len2-1;j