Codeforces Gym 101667H (UVA Live 8095) Rock Paper Scissors (FFT) -2017 Daejeon Regional

本文主要是介绍Codeforces Gym 101667H (UVA Live 8095) Rock Paper Scissors (FFT) -2017 Daejeon Regional,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目传送门

Problem H

Rock Paper Scissors

Time Limit: 1 Second

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 ݊ and your machine also will generates a list of its choice of the length ݉. 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.

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

Input

Your program is to read from standard input. The first line contains two positive integers ݊ and ሺ1 ൑ ݉ ൏݊ ൑ 100,000ሻ, where ݊ is the length of the string for RPS machine and ݉ 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 thelist of choices of your machine.

Output

Your program is to write to standard output. The first line should contain an integer indicating the maximum number of wining matches. The following shows sample input and output for four test cases.



给两个仅由RPS构成的字符串,长短不一,问你把短串从长串某个位置开始一一匹配字符,最多可以匹配多少位。


可以对每一种字符单独计算答案,把字符匹配的过程看做卷积和,则当前要匹配的字符对应1,否则为0,把其中一个串反转,就只要算这两个01数列的卷积和即可。

利用FFT算出每种字符匹配对答案的贡献,最后相加取最大值即可。


#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn = 400005, inf = 0x3f3f3f3f;
const ll llinf = 0x3f3f3f3f3f3f3f3f;
const ld pi = acos(-1.0L);
char s[maxn], t[maxn];
int n1, n2, m;struct complex
{double r, i;complex(double _r = 0, double _i = 0){r = _r; i = _i;}complex operator +(const complex &b){return complex(r + b.r, i + b.i);}complex operator -(const complex &b){return complex(r - b.r, i - b.i);}complex operator *(const complex &b){return complex(r*b.r - i * b.i, r*b.i + i * b.r);}
};
complex conj(complex a) {return complex(a.r, -a.i);
}
complex a[maxn], b[maxn];
int ans[maxn];struct fff {int n, rev[maxn];complex o[maxn], oi[maxn];void init(int m) {n = 1;int k = 0;mem0(rev); mem0(o); mem0(oi);while (n < m) n <<= 1, k++;for (int i = 0; i < n; i++) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (k - 1));for (k = 0; k < n; k++) {o[k] = complex(cos(2 * pi / n * k), sin(2 * pi / n * k));oi[k] = conj(o[k]);}}void fft(complex *a, complex *w) {for (int i = 0; i < n; i++)if (i < rev[i]) swap(a[i], a[rev[i]]);for (int l = 2; l <= n; l <<= 1) {int m = l >> 1;for (complex *p = a; p != a + n; p += l)for (int k = 0; k < m; k++) {complex t = w[n / l * k] * p[k + m];p[k + m] = p[k] - t;p[k] = p[k] + t;}}}void dft(complex *a, int f) {if (f == 1) fft(a, o); else {fft(a, oi);for (int i = 0; i < n; i++) a[i].r /= n;}}void mul(complex *a, complex *b, int m) {init(m);dft(a, 1); dft(b, 1);for (int i = 0; i < n; i++) a[i] = a[i] * b[i];dft(a, -1);}
};
fff f;void cal(char c) {mem0(a); mem0(b);for (int i = 0; i < n1; i++) if (s[i] == c) a[i].r = 1; else a[i].r = 0;for (int i = 0; i < n2; i++) if (t[i] == c) b[i].r = 1; else b[i].r = 0;f.mul(a, b, m);for (int i = 0; i < m; i++) ans[i] += floor(a[i].r + 0.5);
}int main() {int i, j;scanf("%d%d", &n1, &n2);scanf("%s", s); scanf("%s", t);m = n1 + n2 - 1;for (i = 0; i < n1; i++) {if (s[i] == 'R') s[i] = 'P'; elseif (s[i] == 'P') s[i] = 'S'; else s[i] = 'R';}strrev(t);cal('R'); cal('P'); cal('S');int ANS = -1;;for (i = n2-1; i < m; i++) ANS = max(ANS, ans[i]);printf("%d\n", ANS);
//	system("pause");return 0;
}


这篇关于Codeforces Gym 101667H (UVA Live 8095) Rock Paper Scissors (FFT) -2017 Daejeon Regional的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/538665

相关文章

uva 10055 uva 10071 uva 10300(水题两三道)

情歌两三首,水题两三道。 好久没敲代码了为暑假大作战热热身。 uva 10055 Hashmat the Brave Warrior 求俩数相减。 两个debug的地方,一个是longlong,一个是输入顺序。 代码: #include<stdio.h>int main(){long long a, b;//debugwhile(scanf("%lld%lld", &

poj 3259 uva 558 Wormholes(bellman最短路负权回路判断)

poj 3259: 题意:John的农场里n块地,m条路连接两块地,w个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts。 任务是求你会不会在从某块地出发后又回来,看到了离开之前的自己。 判断树中是否存在负权回路就ok了。 bellman代码: #include<stdio.h>const int MaxN = 501;//农场数const int

poj 2349 Arctic Network uva 10369(prim or kruscal最小生成树)

题目很麻烦,因为不熟悉最小生成树的算法调试了好久。 感觉网上的题目解释都没说得很清楚,不适合新手。自己写一个。 题意:给你点的坐标,然后两点间可以有两种方式来通信:第一种是卫星通信,第二种是无线电通信。 卫星通信:任何两个有卫星频道的点间都可以直接建立连接,与点间的距离无关; 无线电通信:两个点之间的距离不能超过D,无线电收发器的功率越大,D越大,越昂贵。 计算无线电收发器D

uva 10387 Billiard(简单几何)

题意是一个球从矩形的中点出发,告诉你小球与矩形两条边的碰撞次数与小球回到原点的时间,求小球出发时的角度和小球的速度。 简单的几何问题,小球每与竖边碰撞一次,向右扩展一个相同的矩形;每与横边碰撞一次,向上扩展一个相同的矩形。 可以发现,扩展矩形的路径和在当前矩形中的每一段路径相同,当小球回到出发点时,一条直线的路径刚好经过最后一个扩展矩形的中心点。 最后扩展的路径和横边竖边恰好组成一个直

uva 10061 How many zero's and how many digits ?(不同进制阶乘末尾几个0)+poj 1401

题意是求在base进制下的 n!的结果有几位数,末尾有几个0。 想起刚开始的时候做的一道10进制下的n阶乘末尾有几个零,以及之前有做过的一道n阶乘的位数。 当时都是在10进制下的。 10进制下的做法是: 1. n阶位数:直接 lg(n!)就是得数的位数。 2. n阶末尾0的个数:由于2 * 5 将会在得数中以0的形式存在,所以计算2或者计算5,由于因子中出现5必然出现2,所以直接一

uva 568 Just the Facts(n!打表递推)

题意是求n!的末尾第一个不为0的数字。 不用大数,特别的处理。 代码: #include <stdio.h>const int maxn = 10000 + 1;int f[maxn];int main(){#ifdef LOCALfreopen("in.txt", "r", stdin);#endif // LOCALf[0] = 1;for (int i = 1; i <=

uva 575 Skew Binary(位运算)

求第一个以(2^(k+1)-1)为进制的数。 数据不大,可以直接搞。 代码: #include <stdio.h>#include <string.h>const int maxn = 100 + 5;int main(){char num[maxn];while (scanf("%s", num) == 1){if (num[0] == '0')break;int len =

uva 10014 Simple calculations(数学推导)

直接按照题意来推导最后的结果就行了。 开始的时候只做到了第一个推导,第二次没有继续下去。 代码: #include<stdio.h>int main(){int T, n, i;double a, aa, sum, temp, ans;scanf("%d", &T);while(T--){scanf("%d", &n);scanf("%lf", &first);scanf

uva 10916 Factstone Benchmark(打表)

题意是求 k ! <= 2 ^ n ,的最小k。 由于n比较大,大到 2 ^ 20 次方,所以 2 ^ 2 ^ 20比较难算,所以做一些基础的数学变换。 对不等式两边同时取log2,得: log2(k ! ) <=  log2(2 ^ n)= n,即:log2(1) + log2(2) + log2 (3) + log2(4) + ... + log2(k) <= n ,其中 n 为 2 ^

uva 10025 The ? 1 ? 2 ? ... ? n = k problem(数学)

题意是    ?  1  ?  2  ?  ...  ?  n = k 式子中给k,? 处可以填 + 也可以填 - ,问最小满足条件的n。 e.g k = 12  - 1 + 2 + 3 + 4 + 5 + 6 - 7 = 12 with n = 7。 先给证明,令 S(n) = 1 + 2 + 3 + 4 + 5 + .... + n 暴搜n,搜出当 S(n) >=