2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 H-Rock Paper Scissors Lizard Spock.

传送门:Rock Paper Scissors Lizard Spock.

这题还有一个弱化版:GYM 101667-H


分析:石头剪刀布加强版。

把短的字符串跟长的字符串从某一位置进行匹配,问最多可以匹配多少位。

把其中一个串反转,对每一种字符单独计算。

字符匹配的过程看做卷积和,则当前要匹配的字符对应1,否则为0。

用FFT快速求解,最后取最大值。


代码如下:

#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;

const int maxn = 4e6+10;
const double PI = acos(-1.0);

struct complex{
     double r,i;
     complex (double r=0.0, double i=0.0):r(r),i(i){}
     complex operator +(const complex &rhs) {return complex(r+rhs.r,i+rhs.i); }
     complex operator -(const complex &rhs) {return complex(r-rhs.r,i-rhs.i); }
     complex operator *(const complex &rhs) {return complex(r*rhs.r-i*rhs.i,r*rhs.i+i*rhs.r); }
};

void change(complex y[], int len) {
    int i,j,k;
    for (int i=1, j = len/2; i<len-1; i++) {
        if (i<j) swap(y[i],y[j]);
        k = len/2;
        while (j>=k) {
            j-=k;
            k/=2;
        }
        if (j<k) j+=k;
    }
}

void fft(complex y[], int len, int on) {
    change(y,len);
    for (int h=2; h<=len; h<<=1) {
        complex wn(cos(-on*2*PI/h),sin(-on*2*PI/h));
        for (int j=0; j<len; j+=h) {
            complex w(1,0);
            for (int k=j; k<j+h/2; k++) {
                complex u = y[k];
                complex t = w*y[k+h/2];
                y[k] = u+t;
                y[k+h/2] = u-t;
                w = w*wn;
            }
        }
    }

    if (on == -1) {
        for (int i=0; i<len; i++) y[i].r /= len;
    }
}

complex x1[maxn],x2[maxn];
char a[maxn/2],b[maxn/2],a1[maxn/2];
int sum[maxn],ans[maxn];
int n1,n2;

void calc(char c){
   int len = 1;
   while (len<n1*2 || len<n2*2) len<<=1;
   for (int i=0; i<n1; i++) x1[i] = complex(a[i]==c,0);
   for (int i=n1; i<len; i++) x1[i] = complex(0,0);
   for (int i=0; i<n2; i++) x2[i] = complex(b[i]==c,0);
   for (int i=n2; i<len; i++) x2[i] = complex(0,0);
   fft(x1,len,1);
   fft(x2,len,1);
   for (int i=0; i<len; i++) x1[i] = x1[i]*x2[i];
   fft(x1,len,-1);
   for (int i=0; i<len; i++) sum[i] = (int)(x1[i].r+0.5);
   len = n1+n2-1;
   for (int i=0; i<len; i++) ans[i] += sum[i];
}

int main(){
    while (scanf("%s%s",a1,b)==2) {
        n1 = strlen(a1);
        n2 = strlen(b);
        for (int i=0; i<=n1+n2-1; i++) ans[i] = 0;
        reverse(b,b+n2);
        for (int i=0; i<n1; i++) {
            if (a1[i] == 'P') a[i] = 'S';
            else if (a1[i] == 'R') a[i] = 'P';
            else if (a1[i] == 'L') a[i] = 'R';
            else if (a1[i] == 'S') a[i] = 'K';
            else if (a1[i] == 'K') a[i] = 'L';
        }
        calc('S');  calc('R'); calc('P');  calc('L'); calc('K');

        for (int i=0; i<n1; i++) {
            if (a1[i] == 'P') a[i] = 'L';
            else if (a1[i] == 'R') a[i] = 'K';
            else if (a1[i] == 'L') a[i] = 'S';
            else if (a1[i] == 'S') a[i] = 'R';
            else if (a1[i] == 'K') a[i] = 'P';
        }
        calc('S');  calc('R'); calc('P');  calc('L'); calc('K');

        int total = 0;
        for (int i=n2-1; i<n1+n2-1; i++) total = max(ans[i],total);
        printf("%d\n",total);
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ACM-ICPC(国际大学程序设计竞赛)是一项全球性的大学程序设计比赛,每年吸引来自世界各地的顶尖大学代表队参与。ACM-ICPC竞赛的核心内容是团队编程和问题解决能力。 首先,ACM-ICPC竞赛对参赛选手的编程能力要求很高。参赛队伍需要在规定的时间内解决一系列的算法问题,这些问题常常包含复杂的数据结构和算法,要求选手在有限的时间内设计和实现高效的程序。 其次,ACM-ICPC竞赛强调团队协作。每个队伍由三名选手组成,他们需要分工合作,保持良好的沟通与协调,共同解决问题。团队成员需要相互理解、相互信任,快速地协商和决策,同时要保持高效的任务分配和时间管理。 此外,ACM-ICPC竞赛也需要选手具备良好的问题解决能力。这些问题往往是实际应用或理论推导相关的,选手需要从数学、计算机科学和算法等多个角度出发,找到最佳解决方案。在面对问题时,选手需要对问题进行分析、抽象和建模,运用各种算法和数据结构进行解决。 对于参赛选手来说,ACM-ICPC提供了一个学习与交流的平台。在比赛中,选手可以接触到不同国家和地区的优秀程序设计人才,学习他们的思维方式和编程技巧。同时,ACM-ICPC还举办了一系列的培训和研讨会,让选手有机会深入了解计算机科学和算法领域最新的研究成果。 总之,ACM-ICPC国际大学程序设计竞赛是一个挑战性与学习性兼具的比赛。它要求选手具备扎实的编程技能、团队合作能力和问题解决能力。参与此竞赛不仅可以锻炼自己的编程能力,还能与全球的顶尖程序设计人才进行交流,拓宽自己的视野和思维方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值