Gym - 101667H - Rock Paper Scissors FFT 求区间相同个数

Gym - 101667H:https://vjudge.net/problem/Gym-101667H

参考:https://blog.csdn.net/weixin_37517391/article/details/80154299

题意:

    已知两个人出剪刀石头布的顺序,第二个人可以选择从第一个人的任意手开始正式比赛,问第二个人赢得个数最多是多少。

思路:

    首先肯定要把第二个人所代表的字符串T转化为对应能赢的字符串rT。这时候暴力的话就是把rT拿去和第一个人的字符串匹配。

 

这里就可以用FFT优化了,FFT的作用就是求两个函数的卷积,令T’为rT的对称,我们试着做一下两个多项式相乘,可以发现新的卷积序列中的第k个位置的值等于S[klenT+1k]T‘序列对应位置乘积之和,所以构造两个函数,每个函数中,若这个位子有对应的字符,就为1,否则为0,利用FFT,就可以求出区间中某个字符匹配的个数。由于我们有三个字符,所以要做三次。

#include <algorithm>
#include  <iterator>
#include  <iostream>
#include   <cstring>
#include   <cstdlib>
#include   <iomanip>
#include   <complex>
#include    <bitset>
#include    <cctype>
#include    <cstdio>
#include    <string>
#include    <vector>
#include     <stack>
#include     <cmath>
#include     <queue>
#include      <list>
#include       <map>
#include       <set>
#include   <cassert>
 
using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000")  //c++
// #pragma GCC diagnostic error "-std=c++11"
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3)
 
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue
 
 
 
typedef long long ll;
typedef unsigned long long ull;
 
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3;
typedef complex<double> cp;
//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n'
 
#define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A)  //用来压行
#define REP(i , j , k)  for(int i = j ; i <  k ; ++i)
#define max3(a,b,c) max(max(a,b), c);
//priority_queue<int ,vector<int>, greater<int> >que;
 
const ll mos = 0x7FFFFFFF;  //2147483647
const ll nmos = 0x80000000;  //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //18
// const int mod = 10007;
const double esp = 1e-8;
const double PI=acos(-1.0);
const double PHI=0.61803399;    //黄金分割点
const double tPHI=0.38196601;
 
 
template<typename T>
inline T read(T&x){
    x=0;int f=0;char ch=getchar();
    while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x=f?-x:x;
}
 
 
/*-----------------------showtime----------------------*/
            const int maxn = 1e5+7;                  //注意不能和len1+len2的和刚刚好,因为n是最近的2的阶乘
            // cp a[maxn<<2], b[maxn<<2], omg[maxn<<2], inv[maxn<<2];
            int n = 1;
           
            cp wn,wntmp;
            void rader(cp arr[],int n){
                int num = n-1;
                for(int i = 0;i < n;++i){
                    int tn = n>>1;
                    while(num && num >= tn) num ^= tn,tn >>= 1;
                    num |= tn;
                    if(num > i) swap(arr[i],arr[num]);
                }
            }
            void FFT(cp cs[],int n,int f){
                rader(cs,n);
                for(int s = 1;s < n;s <<= 1){
                    wn = cp(cos(f*2*PI/(s*2)),sin(f*2*PI/(s*2)));
                    for(int offset = 0;offset < n;offset += s<<1){
                        wntmp = cp(1.0,0.0);
                        for(int i = 0;i < s;++i){
                            cp u = cs[offset+i],v = cs[offset+i+s]*wntmp;
                            cs[offset+i] = u + v;
                            cs[offset+i+s] = u - v;
                            wntmp = wntmp * wn;
                        }
                    }
                }
                if(f == -1)
                    for(int i = 0;i < n;++i)
                        cs[i].real(cs[i].real()/n);
            }

            int len1,len2,ans[maxn<<2];
            char s1[maxn],s2[maxn];
            cp a[maxn<<2], b[maxn<<2];
            void solve(char c){
                memset(a,0,sizeof(a));
                memset(b,0,sizeof(b));
                for(int i=0; i<len1; i++) if(s1[i] == c) a[i] = cp(1.0);
                for(int i=0; i<len2; i++) if(s2[i] == c) b[i] = cp(1.0);
                n=1;
                while(n < len1) n<<=1;
                n<<=1;
                FFT(a,n,1);FFT(b,n,1);
                for(int i=0; i<n; i++)a[i] = a[i] * b[i];
                FFT(a,n,-1);
                for(int i=len2-1; i<n; i++){
                    ans[i] += int(a[i].real() + 0.5);
                }
            }
int main(){

            scanf("%d%d%s%s", &len1, &len2, s1,s2);
            int le = 0,ri = len2-1;
            while(le <= ri) swap(s2[le],s2[ri]),le++,ri--;
            // cout<<s2<<endl;
            for(int i=0; i<len2; i++){
                if(s2[i] == 'S')s2[i] = 'P';
                else if(s2[i] == 'R')s2[i] = 'S';
                else s2[i] = 'R';
            }
            solve('R'),solve('S'),solve('P');
            int mx = 0;
            for(int i=0; i<=len1+len2; i++) mx = max(mx, ans[i]);
            printf("%d\n", mx);
            return 0;
}
Gym-101667H

 

 

转载于:https://www.cnblogs.com/ckxkexing/p/9741586.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值