农民约翰收到一条的消息,记该消息为长度至少为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.

输入输出样例

输入样例#1: 复制

ABABA
输出样例#1: 复制

8

说明

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

题解

设f[i][j]为区间[i,j]的方案数
观察题目我们发现一个新的字符串由两部分组成,一部分是原字符串,另一部分是原字符串的前缀或后缀
我们就枚举断点k更新方案数

初始化:
除了f[1][len]外,都设为1,因为每个子串自身就可能成为一种方案,作为转移的基底

状态转移:
对于断点k,分为了[i,k],[k + 1,j]两段,由题另一部分不能与原串相等,故两段长度不能相等,而且长的那一段就一定是原串,若能判定出另一段是该段的前缀或后缀,就可以统计f[i][j] += f[i][k] 或 f[k + 1][j]

判定相等:
串的长度只有100,我们可以开一个macth[i][j][k]表示[i,i + k - 1]与[j,j + k - 1]是否相等,暴力预处理就好了

#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】的更多相关文章

  1. 洛谷 P3102 [USACO14FEB]秘密代码Secret Code 解题报告

    P3102 [USACO14FEB]秘密代码Secret Code 题目描述 Farmer John has secret message that he wants to hide from his ...

  2. 洛谷P3102 [USACO14FEB]秘密代码Secret Code

    P3102 [USACO14FEB]秘密代码Secret Code 题目描述 Farmer John has secret message that he wants to hide from his ...

  3. 洛谷 P3102 [USACO14FEB]秘密代码Secret Code

    P3102 [USACO14FEB]秘密代码Secret Code 题目描述 Farmer John has secret message that he wants to hide from his ...

  4. P3102 [USACO14FEB]秘密代码Secret Code

    题目描述 Farmer John has secret message that he wants to hide from his cows; the message is a string of ...

  5. 洛谷P2922 [USACO008DEC] 秘密消息Secret Message [Trie树]

    洛谷传送门,BZOJ传送门 秘密消息Secret Message Description     贝茜正在领导奶牛们逃跑.为了联络,奶牛们互相发送秘密信息.     信息是二进制的,共有M(1≤M≤5 ...

  6. 洛谷p2922[USACO08DEC]秘密消息Secret Message

    题目: 题目链接:[USACO08DEC]秘密消息Secret Message 题意: 给定n条01信息和m条01密码,对于每一条密码A,求所有信息中包含它的信息条数和被它包含的信息条数的和. 分析: ...

  7. 洛谷P4302 [SCOI]字符串折叠 [字符串,区间DP]

    题目传送门 字符串折叠 题目描述 折叠的定义如下: 一个字符串可以看成它自身的折叠.记作S = S X(S)是X(X>1)个S连接在一起的串的折叠.记作X(S) = SSSS…S(X个S). 如 ...

  8. 洛谷P1514 引水入城 [搜索,区间DP]

    题目传送门 引水入城 题目描述 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个 N 行×M 列的矩形,如上图所示,其中每个格子都代表一座城市,每 ...

  9. 洛谷 P2922 [USACO08DEC]秘密消息Secret Message

    题目描述 Bessie is leading the cows in an attempt to escape! To do this, the cows are sending secret bin ...

随机推荐

  1. 三、Django安装和流程

    一.MVC模式 MVC(Model-View-Controller),中文名“模型-视图-控制器”,是一个好的Web应用开发所遵循的模式,它有利于把Web应用的代码分解为易于管理的功能模块. M:Mo ...

  2. JS_正则表达式_验证中文字符

    正则表达式:"^[\u4e00-\u9fa5]{0,}$" . "/^[\u4E00-\u9FA5]{1,5}$/" 的含义: 在JS里,\uXXXX 是转义字 ...

  3. ESP8266 station模式下建立client、server TCP连接

    程序实现内容: 1.在station模式下,ESP8266作为client.server进行TCP连接2.实现数据的发送.接收(同时回传)实现思路:TCP网络通信分层为:应用层.网络层.数据链路层.物 ...

  4. 【SpringBoot】集成 Web Flux

    前言: 必需学会SpringBoot基础知识 简介: Takes an opinionated view of building production-ready Spring application ...

  5. 使用Photon引擎进行unity网络游戏开发(一)——Photon引擎简介

    使用Photon引擎进行unity网络游戏开发(一)--Photon引擎简介 Photon PUN Unity 网络游戏开发 Photon引擎简介: 1. 服务器引擎: 服 务 器 引 擎 介 绍 服 ...

  6. 413. Reverse Integer【LintCode java】

    Description Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-b ...

  7. mui搜索框 搜索点击事件

    <div class="mui-input-row mui-search"> <input type="search" class=" ...

  8. 小数第n位:高精度

    小数第n位 问题描述 我们知道,整数做除法时,有时得到有限小数,有时得到无限循环小数. 如果我们把有限小数的末尾加上无限多个0,它们就有了统一的形式. 本题的任务是:在上面的约定下,求整数除法小数点后 ...

  9. NumPy常用函数总结

    转载:https://www.cnblogs.com/hd-chenwei/p/6832732.html NumPy库总包含两种基本的数据类型:矩阵和数组,矩阵的使用类似Matlab,本实例用得多的是 ...

  10. vue cli3 配置postcss

    1.安装postcss-import,postcss-cssnext 包 2.修改package.json 将postcss响应的内容替换为 "postcss": { " ...