zoukankan      html  css  js  c++  java
  • 洛谷 P3102 [USACO14FEB]秘密代码Secret Code 解题报告

    P3102 [USACO14FEB]秘密代码Secret Code

    题目描述

    Farmer John has secret message that he wants to hide from his cows; the message is a string of length at least 2 containing only the characters A..Z.

    To encrypt his message, FJ applies a sequence of "operations" to it, where an operation applied to a string S first shortens S by removing either some (but not all) of the initial characters or some (but not all) of the final characters from S, after which the original string S is attached either at the beginning or end. For example, a single operation to the string ABC could result in eight possible strings:

    AABC ABABC BCABC CABC ABCA ABCAB ABCBC ABCC Given the final encrypted string, please count the number of possible ways FJ could have produced this string using one or more repeated operations applied to some source string. Operations are treated as being distinct even if they give the same encryption of FJ's message. For example, there are four distinct separate ways to obtain AAA from AA.

    Print your answer out modulo 2014.

    农民约翰收到一条的消息,记该消息为长度至少为2,只由大写字母组成的字符串S,他通过一系列操作对S进行加密。

    他的操作为,删除S的前面或者后面的若干个字符(但不删光整个S),并将剩下的部分连接到原字符串S的前面或者后面。如对于S=‘ABC’,共有8总可能的操作结果:

    AABC

    ABABC

    BCABC

    CABC

    ABCA

    ABCAB

    ABCBC

    ABCC

    给出加密后的目标字符串,请计算共有多少种加密的方案。

    对于同字符的字符串,加密方案不止一种,比如把AA加密成AAA,共有4种加密方案。将你的答案mod 2014后输出。

    输入输出格式

    输入格式:

    • Line 1: A single encrypted string of length at most 100.

    输出格式:

    • Line 1: The number of ways FJ could have produced this string with one or more successive operations applied to some initial string of length at least 2, written out modulo 2014. If there are no such ways, output zero.

    说明

    Here are the different ways FJ could have produced ABABA:

    1. Start with ABA -> AB+ABA

    2. Start with ABA -> ABA+BA

    3. Start with AB -> AB+A -> AB+ABA

    4. Start with AB -> AB+A -> ABA+BA

    5. Start with BA -> A+BA -> AB+ABA

    6. Start with BA -> A+BA -> ABA+BA

    7. Start with ABAB -> ABAB+A

    8. Start with BABA -> A+BABA


    不知道为什么最近总感觉(dp)题挺难想的。

    这题大概写了有一个小时,最开始想了一种0/1判左右的方法,发现不行,遂改成区间(DP)

    (dp[i][j])代表长度左区间为(i)右区间为(j)的串的构造方案(注意这题是可以构造很多次的)

    (dp[i][j]=sum dp[i][k_1]+sum dp[k_2][j]),合法性判断参照题目。

    复杂度:(O(N^4)),不过先用Floyd预处理合法性可以优化掉一维。

    其实不优化也差不了太远,毕竟上限远远达不到。

    我交了题目才看到别人写的Floyd优化emmmm


    code:

    #include <cstdio>
    #include <cstring>
    const int mod=2014;
    const int N=105;
    int dp[N][N],n;
    char s[N];
    int main()
    {
        scanf("%s",s+1);
        n=strlen(s+1);
        for(int i=1;i<=n;i++)
            for(int j=i+1;j<=n;j++)
                dp[i][j]=1;
        dp[1][n]=0;
        for(int i=n-1;i;i--)
            for(int j=i+1;j<=n;j++)
            {
                int l=j+1-i,r=l-1>>1;
                for(int k=1;k<=r;k++)
                {
                    int flag=1;
                    int l1=i,l2=i+k;
                    for(int q=k;q;q--,l1++,l2++)
                        if(s[l1]!=s[l2])
                        {
                            flag=0;
                            break;
                        }
                    if(flag)
                        dp[i][j]=(dp[i+k][j]+dp[i][j])%mod;
                    flag=1,l1=i+k-1,l2=j;
                    for(int q=k;q;q--,l1--,l2--)
                        if(s[l1]!=s[l2])
                        {
                            flag=0;
                            break;
                        }
                    if(flag)
                        dp[i][j]=(dp[i][j]+dp[i+k][j])%mod;
                }
                for(int k=1;k<=r;k++)
                {
                    int flag=1;
                    int r1=j,r2=j-k;
                    for(int q=k;q;q--,r1--,r2--)
                        if(s[r1]!=s[r2])
                        {
                            flag=0;
                            break;
                        }
                    if(flag)
                        dp[i][j]=(dp[i][j]+dp[i][j-k])%mod;
                    flag=1,r1=i,r2=j-k+1;
                    for(int q=k;q;q--,r1++,r2++)
                        if(s[r1]!=s[r2])
                        {
                            flag=0;
                            break;
                        }
                    if(flag)
                        dp[i][j]=(dp[i][j]+dp[i][j-k])%mod;
                }
            }
        printf("%d
    ",dp[1][n]%mod);
        return 0;
    }
    
    

    2018.6.12

  • 相关阅读:
    Axure RP 9 Beta 开放下载(更新激活密钥和汉化包)
    Android 解决NestedScrollView 嵌套 RecyclerView出现的卡顿,上拉刷新无效
    上周热点回顾(8.27-9.2)团队
    上周热点回顾(8.20-8.26)团队
    上周热点回顾(8.13-8.19)团队
    上周热点回顾(8.6-8.12)团队
    阿里云MVP第6期招募与博客园合作启动团队
    上周热点回顾(7.30-8.5)团队
    上周热点回顾(7.23-7.29)团队
    上周热点回顾(7.16-7.22)团队
  • 原文地址:https://www.cnblogs.com/butterflydew/p/9171451.html
Copyright © 2011-2022 走看看