Secret Code

一、题目

【NOIP模拟赛A10】Secret Code

时间限制: 1 Sec  内存限制: 128 MB
提交: 10  解决: 6
[提交][状态][讨论版]

题目描述

农夫John(以后简称“FJ”)有些不想让他的奶牛看见的秘密消息;这条消息是一个长度至少为2仅包含字符A..Z的字符串。为了加密他的消息,FJ对这条消息进行了一系列“操作”。一次对字符串S的“操作”是指去掉S开头的若干个字母(但不是全部)或末尾的若干个字母(但不是全部),然后将原来的S拼接到其开头或末尾。例如,一次对于字符串ABC的操作可能有以下8种结果:

AABC

ABABC

BCABC

CABC

ABCA

ABCAB

ABCBC

ABCC

已知最终加密后的字符串,请计算FJ有多少种可能的方法对某一源字符串进行一次或多次重复的“操作”得到该加密字符串。尽管对一个字符串的不同方法的“操作”可能得到相同的结果,这些方式也应该被视作不同而被分别计数。例如,有4种不同的操作方法从AA得到AAA。

输入

第1行:一个长度不超过100的字符串。

输出

第1行:FJ对某个长度至少为2的源字符串进行一次或多次操作后能够得到该结果字符串的不同方法数,答案可能会很大,答案请对2014求余。如果没有方法得到结果字符串,则输出0。

样例输入

ABABA

样例输出

8

提示

以下是FJ可以得到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为终点的字符串出现的次数
* 最终状态:
* f[1][n]
* 初始状态:
* 初始结果字符串的所有的子字符串出现的次数都为1
* 因为我们是要用子串得到别的串,而这个子串最开始出现的次数就为1
* 状态转移方程:
* f[i][j]+=f[i][k]*f[k+1][j] (i<=k<j)(并且满足短串是长串的头尾字串);
*
* f[1][3]=f[1][1]*f[2][3]+f[1][2]*f[3][3];
* 短的串要是长的串的子串(并且是那种只截取前面或者只截取后面的那种)
*/ #include <iostream>
#include <cstring>
using namespace std;
int f[][];
char s[];
int len; void printRead();
void readData(){
scanf("%s",s+);
len=strlen(s+);
} void printArr_f();
void initArr_f(){
//因为任意一个长度不为Len的串都可以作为起始串
//所以任意一个长度不为Len的串起始可能性都为1
for(int i=;i<=len;i++){
for(int j=i;j<=len;j++){
f[i][j]=;
}
}
//因为必须要截掉头尾,故长度为len的串不行
//我不可能去用1-n这个字符串去得到别的字符串吧,所以出现次数为0
f[][len]=;
} //求字符串(c,d)是不是(a,b)的前子字符串
bool frontSubstring(){ } //求字符串(c,d)是不是(a,b)的后子字符串
bool backSubstring(){ } void init(){
readData();
printRead();
initArr_f();
printArr_f();
} void dp(){
for(int i=len;i>=;i++){
for(int j=i+;j<=len;j++){
for(int k=i;k<=j;k++){
f[i][j]+=f[i][k]*f[k+][j];
}
}
}
} int main(){
freopen("src/in.txt","r",stdin);
init();
dp();
return ;
} void printRead(){
for(int i=;i<=len;i++){
cout<<s[i];
}
cout<<endl;
} void printArr_f(){
for(int i=;i<=len;i++){
for(int j=;j<=len;j++){
cout<<f[i][j]<<" ";
}
cout<<endl;
}
}

AC代码

 #include<bits/stdc++.h>
using namespace std;
char s[];
int ans[][];
//ans[i][j]中存储从s中的第i个元素到第j个元素有多少种出现的可能
bool Front_Maybe(int a,int b,int c,int d)
{
for(int i = a;i <= b;i ++)
{
if(s[i] != s[c + i - a]) return ;
}
// printf("前面%d %d %d %d\n",a,b,c,d);
return ;
}
bool Behind_Maybe(int a,int b,int c,int d)
{
for(int i = b;i >= a;i --)
{
if(s[i] != s[d - b + i]) return ;
}
// printf("后面%d %d %d %d\n",a,b,c,d);
return ;
}
int main()
{
freopen("src/in.txt","r",stdin);
scanf("%s",s + );
int Len = strlen(s + );
for(int i = ;i < Len;i ++)//起始串的长度
{
for(int j = ;j <= Len;j ++)//起始串的起点
{
//i是长度,j是起点,那么i+j-1就是终点
//如果终点大于长度
if(i + j - > Len) continue;
ans[j][i + j - ] = ;
//因为任意一个长度不为Len的串都可以作为起始串
//所以任意一个长度不为Len的串起始可能性都为1
}
}
for(int i = ;i < Len;i ++)//当前要处理串的长度
{
for(int j = ;j <= Len;j ++)//当前要处理串的起点
{
if(i + j - > Len) continue;
for(int x = ;x < i;x ++)//添加串的长度
{
if(j - x > )//向后加处理串 起点合法
{
if(Front_Maybe(j - x,j - ,j,j + i - ))
{
ans[j - x][i + j - ] += ans[j][i + j - ];
}
if(Behind_Maybe(j - x,j - ,j,j + i - ))
{
ans[j - x][i + j - ] += ans[j][i + j - ];
}
ans[j - x][i + j - ] %= ;
}
if(i + j - + x <= Len)//向前加处理串 终点合法
{
if(Front_Maybe(j + i,j + i - + x,j,j + i - ))
{
ans[j][i + j - + x] += ans[j][i + j - ];
}
if(Behind_Maybe(j + i,j + i - + x,j,j + i - ))
{
ans[j][i + j - + x] += ans[j][i + j - ];
}
ans[j][i + j - + x] %= ;
}
}
}
}
printf("%d",ans[][Len]);
return ;
}

Secret Code的更多相关文章

  1. Android Secret Code

    我们很多人应该都做过这样的操作,打开拨号键盘输入*#*#4636#*#*等字符就会弹出一个界面显示手机相关的一些信息,这个功能在Android中被称为android secret code,除了这些系 ...

  2. hdu.1111.Secret Code(dfs + 秦九韶算法)

    Secret Code Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  3. Android 编程下的 Secret Code

    我们很多人应该都做过这样的操作,打开拨号键盘输入 *#*#4636#*#* 等字符就会弹出一个界面显示手机相关的一些信息,这个功能在 Android 中被称为 Android Secret Code, ...

  4. The secret code

    The secret code Input file: stdinOutput file: stTime limit: 1 sec Memory limit: 256 MbAfter returnin ...

  5. [swustoj 679] Secret Code

    Secret Code 问题描述 The Sarcophagus itself is locked by a secret numerical code. When somebody wants to ...

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

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

  7. HDU 1111 Secret Code(数论的dfs)

    Secret Code Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit ...

  8. 【微信】根据appid, secret, code获取用户基本信息

    function getUserInfo(){ $appid = "yourappid"; $secret = "yoursecret"; $code = $_ ...

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

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

随机推荐

  1. python错误笔记

    1.print "hello world!";SyntaxError:Missing parentheses in call to ‘paint’ . Did you mean p ...

  2. 【python】-- Django 分页 、cookie、Session、CSRF

    Django  分页 .cookie.Session.CSRF 一.分页 分页功能在每个网站都是必要的,下面主要介绍两种分页方式: 1.Django内置分页 from django.shortcuts ...

  3. 关于handler内存泄露的问题

    在使用Handler更新UI的时候.我是这样写的: public class SampleActivity extends Activity { private final Handler mLeak ...

  4. mprotect() failed: Cannot allocate memory

    遇到这个问题是在測试项目的性能时发现的,每一个对象分配一页大小的空间然后mprotect() 保护起来,当系统分配3W多个页的时候会出现这个问题. google到在一份邮件列表中也曾提到该问题.htt ...

  5. 基于爬山算法求解TSP问题(JAVA)

    一.TSP问题 TSP问题(Travelling Salesman Problem)即旅行商问题,又译为旅行推销员问题.货郎担问题,是数学领域中著名问题之一.假设有一个旅行商人要拜访n个城市,他必须选 ...

  6. 017-Hadoop Hive sql语法详解7-去重排序、数据倾斜

    一.数据去重排序 1.1.去重 distinct与group by 尽量避免使用distinct进行排重,特别是大表操作,用group by代替 -- 不建议 select DISTINCT key ...

  7. spring项目报org.apache.tiles.definition.DefinitionsFactoryException: I/O错误原因及解决办法。

    今天升级一个spring项目遇到如下错: HTTP Status 500 - Request processing failed; nested exception is org.apache.til ...

  8. 剑指offer 面试49题

    面试49题: 题:丑数 题目:把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含因子7. 习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第N ...

  9. WebMvcRegistrationsAdapter

    27.1.1. Spring MVC自动配置 如果希望使用自定义的RequestMappingHandlerMapping,RequestMappingHandlerAdapter,或Exceptio ...

  10. python基础知识——包

    包是一种通过使用“模块名”来组织python模块的名称空间的方式. 无论是import形式还是from...import形式,凡是在导入语句中(不是在使用时)遇到带点的,就需要意识到——这是包. 包是 ...