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.

输入输出样例

输入样例#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
#include<iostream>
#include<cstdio>
#include<cstring>
#define mod 23333333
using namespace std;
int ans;
char s[],c[],ss[];
bool vis[];
int Hash(char ch[],int len){
int res=;
for(int i=;i<=len;i++)
res=(res*+(ch[i]-'A'+))%mod;
return res;
}
int main(){
scanf("%s",s+);
int len=strlen(s+);
vis[Hash(s,len)]=;
int now;
for(int i=;i<len;i++){//枚举剪下来的字符串的长度
//从前面剪
//接到后面
for(int k=;k<=len;k++)ss[k]=s[k];
for(int k=;k<=i;k++)ss[len+k]=s[k];
now=Hash(ss,len+i);
if(!vis[now]){
vis[now]=;
ans++;
if(ans>=)ans-=;
}
//接到前面
for(int k=;k<=i;k++)ss[k]=s[k];
for(int k=;k<=len;k++)ss[k+i]=s[k];
now=Hash(ss,len+i);
if(!vis[now]){
vis[now]=;
ans++;if(ans>=)ans-=;
}
//从后面剪
//接到后面
for(int k=;k<=len;k++)ss[k]=s[k];
for(int k=,j=len;k<=i;k++,j--)ss[len+k]=s[j];
now=Hash(ss,len+i);
if(!vis[now]){
vis[now]=;
ans++;if(ans>=)ans-=;
}
//接到前面
for(int k=,j=len;k<=i;k++,j--)ss[k]=s[j];
for(int k=;k<=len;k++)ss[k+i]=s[k];
now=Hash(ss,len+i);
if(!vis[now]){
vis[now]=;
ans++;if(ans>=)ans-=;
}
}
printf("%d",ans);
}

20分 不知道为啥写的哈希wa了

/*
substr是C++语言函数,主要功能是复制子字符串,要求从指定位置开始,并具有指定的长度。
*/
#include<bits/stdc++.h>
using namespace std;
string s;
map<string,int>f;
int find(string x){
if(f[x]!=)return f[x];
int tmp=;
int l=x.size();
for(int i=;i*<l;i++){
if(x.substr(,i)==x.substr(l-i,i))
tmp+=find(x.substr(i,l-i))+find(x.substr(,l-i));
if(x.substr(,i)==x.substr(i,i))
tmp+=find(x.substr(i,l-i));
if(x.substr(l-*i,i)==x.substr(l-i,i))
tmp+=find(x.substr(,l-i));
}
return f[x]=tmp%;
}
int main(){
cin>>s;
cout<<(find(s)+-)%;
return ;
}

100分 STL+记忆化搜索

 

最新文章

  1. Moving Average from Data Stream
  2. sqoop的使用
  3. url编码 中文在url参数中传递,在请求头,响应头中传递,是如何编码的呢?
  4. Linux之文件系统的简单操作
  5. 如何验证 jemalloc 优化 Nginx 是否生效
  6. 第二百二十八天 how can I 坚持
  7. C语言链表各类操作详解
  8. WeasyPrint - Converts HTML + CSS to PDF - WeasyPrint converts HTML/CSS documents to PDF
  9. 一道TOPK问题
  10. 100本最棒的web前端图书推荐
  11. angularjs上传图片和文件
  12. 参考用bat文件
  13. mysql的学习笔记(六)
  14. 【转】iOS弹幕库OCBarrage-如何hold住每秒5000条巨量弹幕
  15. Bagging, Boosting, Bootstrap
  16. 代理_正向代理_反向代理_nginx_转
  17. Google API Design Guide (谷歌API设计指南)中文版
  18. Orleans学习总结(二)--创建工程
  19. hbase-java-api002(flush)
  20. socket 聊天室实现

热门文章

  1. Java_util_02_Java判断字符串是中文还是英文
  2. FFMPEG实现的转码程序
  3. SPOJ8093Sevenk Love Oimaster(广义后缀自动机)
  4. ACM学习历程—HDU 5073 Galaxy(数学)
  5. Codeforces Round #402 (Div. 2) 阵亡记
  6. iOS中的日期和时间
  7. 11g RAC 如何备份OCR,利用备份恢复OCR,ocrdump
  8. Java程序打包成exe可执行文件
  9. Java常见设计模式之适配器模式
  10. .net 缓存之数据库缓存依赖