1. 程式人生 > >Rock Paper Scissors+fft

Rock Paper Scissors+fft

問題 H: Rock Paper Scissors

時間限制: 1 Sec  記憶體限制: 128 MB 提交: 36  解決: 5 [提交] [狀態] [討論版] [命題人:admin]

題解:列舉你出的每一種拳,將其設為 1, 其他的設為 0 ,將電腦序列中你能贏的拳設為 1, 其他的設為 0

,則對於每一個pos,只比較你的該種出拳能夠勝利的局數為

然後把你的序列反過來,就變成了卷積的形式:

做三遍 FFT 即可。

題目描述

There is a Rock Paper Scissors (RPS) machine which generates Rock, Paper, or Scissors randomly. You also have a similar small Rock Paper Scissors machine. Before the game, the RPS machine will generate a list of its choice of Rock, Paper, or Scissors of the length n and your machine also will generates a list of its choice of the length m. That is, you know the whole list of the RPS’s choices and you have the list of your machine’s choices. Of course, each choice of the machines is one of the three options (Rock, Paper, or Scissors). Now, you start playing Rock Paper Scissors game. In every match, you compare the list of RPS’s choice and the list of your machine’s in sequence and decide whose machine would win. However, only you may skip some RPS’s choices to find the position to get the most winning points of your machine. After you decide to start match you cannot skip the match till the end of the match. ‘R’ stands for Rock, ‘P’ stands for Paper, and ‘S’ stands for Scissors. For example, suppose that the RPS’s list is “RSPPSSSRRPPR” and your machine’s list is “RRRR”. To get the most winning points, you should start the match after skipping three RPS’s choices or four RPS’s choices. (See Figure H.1.) Then, you can win in three matches. The draw case is not considered.

Figure H.1. The most winning position against RPS machine when n = 12 and m = 4.

Given the list of RPS’s choices and the list of your choices, find the position to get the maximum number of wining matches.

輸入

Your program is to read from standard input. The first line contains two positive integers n and m (1 ≤ m < n ≤ 100,000), where n is the length of the string for RPS machine and m is the length of the string for your machine. Following the first line contains the list of choices of RPS machine and the second line contains the list of choices of your machine.

輸出

Your program is to write to standard output. The first line should contain an integer indicating the maximum number of wining matches

樣例輸入

12 4
RSPPSSSRRPPR
RRRR

樣例輸出

3
scanf("%d %d",&n,&m);
	scanf("%s %s",s,t);
	
	reverse(t,t+m);
	int len=(1<<18);
	initRev(len,18);
	for(int i=0;i<3;i++){
		for(int i=0;i<len;i++)
			A[i].x=A[i].y=B[i].x=B[i].y=0;
		for(int j=0;j<n;j++)
			if(s[j]==h[i+1])A[j].x+=1;
		for(int j=0;j<m;j++)
			if(t[j]==h[i])B[j].x+=1;
		FFT(A,len,1),FFT(B,len,1);
		for(int j=0;j<len;j++)
		A[j]=A[j]*B[j];
		FFT(A,len,-1);
		for(int j=0;j<len;j++)
		c[j]+=round(A[j].x);
	}
	int ans=0;
	for(int i=m-1;i<len;i++)
	ans=max(ans,c[i]);
	printf("%d\n",ans);
	return 0;
}