洛谷 P3102 [USACO14FEB]秘密代码Secret Code 【区间dp】
农民约翰收到一条的消息,记该消息为长度至少为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
题解
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long int
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define fo(i,x,y) for (int i = (x); i <= (y); i++)
#define Redge(u) for (int k = head[u]; k != -1; k = edge[k].next)
using namespace std;
const int maxn = 105,maxm = 100005,INF = 1000000000,P = 2014; inline int read(){
int out = 0,flag = 1;char c = getchar();
while (c < 48 || c > 57) {if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57) {out = out * 10 + c - 48; c = getchar();}
return out * flag;
} bool md[maxn][maxn][maxn];
int f[maxn][maxn],len;
char s[maxn]; int main()
{
scanf("%s",s + 1);
len = strlen(s + 1);
if (len == 2){
cout<<0<<endl;
return 0;
}
REP(i,len) REP(j,len){
for (int k = 1; i + k - 1 <= len && j + k - 1 <= len; k++)
if (s[i + k - 1] == s[j + k - 1]) md[i][j][k] = true;
else break;
}
fill(f[0],f[0] + maxn * maxn,1);
for (int L = 3; L <=len ; L++)
for (int i = 1; i + L - 1 <= len; i++){
int j = i + L - 1;
for (int k = i; k < j; k++){
int len1 = k - i + 1,len2 = j - k;
if (len1 < len2 && md[i][k + 1][len1])
f[i][j] = (f[i][j] + f[k + 1][j]) % P;
if (len1 < len2 && md[i][j - len1 + 1][len1])
f[i][j] = (f[i][j] + f[k + 1][j]) % P;
if (len1 > len2 && md[i][k + 1][len2])
f[i][j] = (f[i][j] + f[i][k]) % P;
if (len1 > len2 && md[k - len2 + 1][k + 1][len2])
f[i][j] = (f[i][j] + f[i][k]) % P;
}
}
cout<<f[1][len] - 1<<endl;
return 0;
}
洛谷 P3102 [USACO14FEB]秘密代码Secret Code 【区间dp】的更多相关文章
- 洛谷 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
P3102 [USACO14FEB]秘密代码Secret Code 题目描述 Farmer John has secret message that he wants to hide from his ...
- 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,求所有信息中包含它的信息条数和被它包含的信息条数的和. 分析: ...
- 洛谷P4302 [SCOI]字符串折叠 [字符串,区间DP]
题目传送门 字符串折叠 题目描述 折叠的定义如下: 一个字符串可以看成它自身的折叠.记作S = S X(S)是X(X>1)个S连接在一起的串的折叠.记作X(S) = SSSS…S(X个S). 如 ...
- 洛谷P1514 引水入城 [搜索,区间DP]
题目传送门 引水入城 题目描述 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个 N 行×M 列的矩形,如上图所示,其中每个格子都代表一座城市,每 ...
- 洛谷 P2922 [USACO08DEC]秘密消息Secret Message
题目描述 Bessie is leading the cows in an attempt to escape! To do this, the cows are sending secret bin ...
随机推荐
- 我们一起学习WCF 第五篇数据协定和消息协定
A:数据协定(“数据协定”是在服务与客户端之间达成的正式协议,用于以抽象方式描述要交换的数据. 也就是说,为了进行通信,客户端和服务不必共享相同的类型,而只需共享相同的数据协定. 数据协定为每个参数或 ...
- Eclipse各个版本
Eclipse最初是由IBM公司开发的替代商业软件Visual Age for Java的下一代IDE开发环境,2001年11月贡献给开源社区,现在它由非营利软件供应商联盟Eclipse基金会. Ec ...
- 利用webbrowser自动查取地点坐标
概述 有时候我们需要去查询某些地点的坐标,那么我们可以用百度提供的坐标拾取系统http://api.map.baidu.com/lbsapi/getpoint/index.html,但是会发现它只能一 ...
- Visual Studio设置字体及护眼背景色
打开vs 菜单栏选择: 工具 -> 选择 -> 环境 -> 字体和颜色,如图所示 字体可以如上选择,背景色选择项背景,点击自定义,如下设置即可.
- 使用GitLab创建项目
- thinkphp5框架生成二维码(二)
上篇已经讲过了SDK之类的,这个不再重复,有不知道的童鞋们,请去看上篇文章吧. 这里我用的方法比较老旧,大家有更好的方法,可以进行改良,还有linux服务器,记得给文件权限,否则生成的文件会失败的.大 ...
- [C++基础] tips
1. 在g++ 中使支持C++11 https://askubuntu.com/questions/773283/how-do-i-use-c11-with-g This you can do by ...
- VLP16线用户手册.md
VLP16线用户手册 文档 传感器数据 分组类型和定义 传感器产生两种类型的数据包:数据包和位置数据包.位置包有时也被称为遥测包或GPS包. 数据包包括传感器测量到的三维数据以及返回光脉冲的表面的校 ...
- component-scan标签的use-default-filters属性的作用以及原理分析
一.背景 我们在Spring+SpringMVC+Mybatis的集成开发中,经常会遇到事务配置不起作用等问题,有时候就是因为包扫描出了问题,其中component-scan的标签的use-def ...
- RESTful源码笔记之RESTful Framework的基本组件
快速实例 Quickstart 序列化 创建一个序列化类 简单使用 开发我们的Web API的第一件事是为我们的Web API提供一种将代码片段实例序列化和反序列化为诸如json之类的表示形式的方式. ...