洛谷 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:
Start with ABA -> AB+ABA
Start with ABA -> ABA+BA
Start with AB -> AB+A -> AB+ABA
Start with AB -> AB+A -> ABA+BA
Start with BA -> A+BA -> AB+ABA
Start with BA -> A+BA -> ABA+BA
Start with ABAB -> ABAB+A
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\n",dp[1][n]%mod);
return 0;
}
2018.6.12
洛谷 P3102 [USACO14FEB]秘密代码Secret Code 解题报告的更多相关文章
- 洛谷P3102 [USACO14FEB]秘密代码Secret Code
P3102 [USACO14FEB]秘密代码Secret Code 题目描述 Farmer John has secret message that he wants to hide from his ...
- 洛谷 P3102 [USACO14FEB]秘密代码Secret Code
P3102 [USACO14FEB]秘密代码Secret Code 题目描述 Farmer John has secret message that he wants to hide from his ...
- 洛谷 P3102 [USACO14FEB]秘密代码Secret Code 【区间dp】
农民约翰收到一条的消息,记该消息为长度至少为2,只由大写字母组成的字符串S,他通过一系列操作对S进行加密. 他的操作为,删除S的前面或者后面的若干个字符(但不删光整个S),并将剩下的部分连接到原字符串 ...
- P3102 [USACO14FEB]秘密代码Secret Code
题目描述 Farmer John has secret message that he wants to hide from his cows; the message is a string of ...
- 洛谷P2922 [USACO008DEC] 秘密消息Secret Message [Trie树]
洛谷传送门,BZOJ传送门 秘密消息Secret Message Description 贝茜正在领导奶牛们逃跑.为了联络,奶牛们互相发送秘密信息. 信息是二进制的,共有M(1≤M≤5 ...
- 洛谷p2922[USACO08DEC]秘密消息Secret Message
题目: 题目链接:[USACO08DEC]秘密消息Secret Message 题意: 给定n条01信息和m条01密码,对于每一条密码A,求所有信息中包含它的信息条数和被它包含的信息条数的和. 分析: ...
- 洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur 解题报告
P3119 [USACO15JAN]草鉴定Grass Cownoisseur 题目描述 约翰有\(n\)块草场,编号1到\(n\),这些草场由若干条单行道相连.奶牛贝西是美味牧草的鉴赏家,她想到达尽可 ...
- 【洛谷】CYJian的水题大赛 解题报告
点此进入比赛 \(T1\):八百标兵奔北坡 这应该是一道较水的送分题吧. 理论上来说,正解应该是DP.但是,.前缀和优化暴力就能过. 放上我比赛时打的暴力代码吧(\(hl666\)大佬说这种做法的均摊 ...
- 洛谷 P2922 [USACO08DEC]秘密消息Secret Message
题目描述 Bessie is leading the cows in an attempt to escape! To do this, the cows are sending secret bin ...
随机推荐
- Luogu P2827 蚯蚓
看到题目就可以想到直接开的堆模拟的过程了吧,这个还是很naive的 注意在用堆做的时候也是要明智一点的,对于蚯蚓长度的相加肯定不能直接遍历并加上,还是可以差分一下的 其实说白了就是把集体加->单 ...
- Luogu P3388 【模板】割点(割顶)
一道求割点的板子题.还是采用经典的Tarjan算法. 首先大致和Tarjan求强连通分量相似,都是用\(dfn_x\)表示访问到\(x\)的时间(时间戳),\(low_x\)表示通过\(x\)回边能走 ...
- 如何写好一篇高质量的IEEE/ACM Transaction级别的计算机科学论文?
转自<知乎>如何写好一篇高质量的IEEE/ACM Transaction级别的计算机科学论文? 问题: 作为一个博士生,一直为写论文头疼,读过很多高质量论文,觉得写的真好,但是轮到自己写总 ...
- BodeAbp服务端介绍
BodeAbp服务端只提供api,绝大部分api通过abp的动态WebApi机制提供,原理可以参考这篇文章:http://www.cnblogs.com/1zhk/p/5418694.html 与业务 ...
- 从0到1上线一个微信小程序
0.0 前期准备 微信小程序的出现极大地降低了个人开发者微创业的门槛,不需要后端技术,不需要服务器和域名这些乱七八糟的前置操作,只需要懂得前端技术,就能发布一款属于自己的轻量级应用,简直是前端开发者的 ...
- Dell BOSS 卡是什么
全名: Boot Optimized Storage Solution 针对 M.2 接口的 SSD,主板上必须设计接口进行适配. 设计一款主板对于硬件厂商来说是有成本的,其中包括 主板设计成本 产品 ...
- ACM-ICPC 2018 沈阳赛区网络预赛 G. Spare Tire
这题很好啊,好在我没做出来...大概分析了一下,题目大概意思就是求 问所有满足1<=i<=n且i与m互素的ai之和 最开始我们队的做法是类似线性筛的方法去筛所有数,把数筛出来后剩下数即可, ...
- Individual P1: Preparation
Individual Project - Word frequency program tally the frequency of words under a directory (2 modes) ...
- Java多线程的使用以及原理
Java有两种方式实现多线程. 第一种——继承Thread类,并重写run方法 步骤: 定义类继承Thread类: 重写子类的run方法,将线程需要执行的代码写在run方法中: 创建子类的对象,则创建 ...
- beta阶段性能指标测试
性能指标概况 安装耗时 启动耗时 CPU占用 内存占用 电池温度 网络流量 平均值 5.48s 1.04s 1.61% 18.68MB 32.44℃ 93.78B 峰值 131.74s 5.13s 5 ...