CodeForces - 156C:Cipher (不错的DP)
Sherlock Holmes found a mysterious correspondence of two VIPs and made up his mind to read it. But there is a problem! The correspondence turned out to be encrypted. The detective tried really hard to decipher the correspondence, but he couldn't understand anything.
At last, after some thought, he thought of something. Let's say there is a word s, consisting of |s| lowercase Latin letters. Then for one operation you can choose a certain position p (1 ≤ p < |s|) and perform one of the following actions:
- either replace letter sp with the one that alphabetically follows it and replace letter sp + 1 with the one that alphabetically precedes it;
- or replace letter sp with the one that alphabetically precedes it and replace letter sp + 1 with the one that alphabetically follows it.
Let us note that letter "z" doesn't have a defined following letter and letter "a" doesn't have a defined preceding letter. That's why the corresponding changes are not acceptable. If the operation requires performing at least one unacceptable change, then such operation cannot be performed.
Two words coincide in their meaning iff one of them can be transformed into the other one as a result of zero or more operations.
Sherlock Holmes needs to learn to quickly determine the following for each word: how many words can exist that coincide in their meaning with the given word, but differs from the given word in at least one character? Count this number for him modulo 1000000007 (109 + 7).
Input
The input data contains several tests. The first line contains the only integer t (1 ≤ t ≤ 104) — the number of tests.
Next t lines contain the words, one per line. Each word consists of lowercase Latin letters and has length from 1 to 100, inclusive. Lengths of words can differ.
Output
For each word you should print the number of different other words that coincide with it in their meaning — not from the words listed in the input data, but from all possible words. As the sought number can be very large, print its value modulo 1000000007 (109 + 7).
Examples
1
ab
1
1
aaaaaaaaaaa
0
2
ya
klmbfxzb
24
320092793
题意:给定长度小于100的字符串,每次操作可以把相邻的字符对,一个+1,一个-1,但要保证所有字符在'a'到'z'范围里。求原字符串可以转化为多少种字符串。
思路:注意到相邻的一个+1,一个-1,之和是不变的,而且不难证明长度相同,之和相同的时候可以相互转化。所以对于对应长度、对应和,其种类是一定的,我们直接预处理出有多少种。dp[Len][sum]表示长度为Len的时候之和为sum的种类数。 避免讨论,我们把a-z对应为1-26,而不是0-25。
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ll long long
using namespace std;
const int maxn=;
const int Mod=1e9+;
int dp[maxn][maxn*],sum[maxn]; char c[maxn];
void solve()
{
dp[][]=;
rep(i,,){
rep(j,i,i*){
rep(p,,){
if(j-p>=) (dp[i][j]+=dp[i-][j-p])%=Mod;
}
}
}
}
int main()
{
int T,N;
solve();
scanf("%d",&T);
while(T--){
scanf("%s",c+); N=strlen(c+);
rep(i,,N) sum[i]=sum[i-]+c[i]-'a'+;
printf("%d\n",(dp[N][sum[N]]+Mod-)%Mod);
}
return ;
}
CodeForces - 156C:Cipher (不错的DP)的更多相关文章
- [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆)
[BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆) 题面 一棵二叉树的所有点的点权都是给定的集合中的一个数. 让你求出1到m中所有权 ...
- codeforces 721C (拓排 + DP)
题目链接:http://codeforces.com/contest/721/problem/C 题意:从1走到n,问在时间T内最多经过多少个点,按路径顺序输出. 思路:比赛的时候只想到拓排然后就不知 ...
- codeforces 711C Coloring Trees(DP)
题目链接:http://codeforces.com/problemset/problem/711/C O(n^4)的复杂度,以为会超时的 思路:dp[i][j][k]表示第i棵数用颜色k涂完后bea ...
- codeforces 55D - Beautiful numbers(数位DP+离散化)
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- Codeforces 543D. Road Improvement (树dp + 乘法逆元)
题目链接:http://codeforces.com/contest/543/problem/D 给你一棵树,初始所有的边都是坏的,要你修复若干边.指定一个root,所有的点到root最多只有一个坏边 ...
- Codeforces 467C. George and Job (dp)
题目链接:http://codeforces.com/contest/467/problem/C 求k个不重叠长m的连续子序列的最大和. dp[i][j]表示第i个数的位置个序列的最大和. 前缀和一下 ...
- Codeforces 711 C. Coloring Trees (dp)
题目链接:http://codeforces.com/problemset/problem/711/C 给你n棵树,m种颜色,k是指定最后的完美值.接下来一行n个数 表示1~n树原本的颜色,0的话就是 ...
- Codeforces 706 C. Hard problem (dp)
题目链接:http://codeforces.com/problemset/problem/706/C 给你n个字符串,可以反转任意一个字符串,反转每个字符串都有其对应的花费ci. 经过操作后是否能满 ...
- CodeForces 163A Substring and Subsequence dp
A. Substring and Subsequence 题目连接: http://codeforces.com/contest/163/problem/A Description One day P ...
随机推荐
- asp.net,关于Listview+DataPager控件使用
关于Listview+DataPager控件使用1.DAL层,根据开始条数+结束条数查询数据.2.BLL层,startRowIndex和maximumRows进行查询.(startRowIndex + ...
- Swift进阶 - 12个技巧
听说你已经学习Swift几个月了,有没有想更进一步成为Swift高手的想法?我这里有11招秘技,各位施主且听我慢慢道来,结个善缘. 1. 扩展(Extension) 任务: 求数字的平方. // 菜鸟 ...
- 用requests登录微信网页版,并接收发送消息
首先,网页版微信登录大致分为以下几个流程(都是大家可以通过抓包得到): 1.登陆主页后,会生成一个UUID,这是个用户标识,在后面请求二维码会用到 def get_uuid(self): '''获取u ...
- RHCE学习笔记 管理1 (第六章 第七章)
第六章 利用linux 文件系统权限文件访问 1.linux文件系统权限 文件的权限分为: rwx 读/写/执行 ls -l /home 查看/home下文件 ls -ld /home ...
- EXTJS4.2 级联 下拉
items: [ { xtype: "fieldcontainer", layout: "hbox", items: [{ xtype: 'combo', na ...
- hdu 1695 GCD 莫比乌斯
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- monkey参数应用
1.指定seed值 adb shell monkey -v -p package -s 100 100 2.touch事件(参数后都跟百分比) 3.设定动作百分比 4.轨迹球 5.基本导航事件 输入 ...
- 使用Putty和Xshell远程登录之密钥认证
本次实验主要使用目前使用最多的Putty和Xshell工具进行实验 关于SSH密钥认证原理,请参考链接:http://www.cnblogs.com/ImJerryChan/p/6661815.htm ...
- Spring Cloud实战微服务入门
1.spring cloud是什么? 是一个快速构建分布式系统的工具集,构建于Spring Boot之上 2.spring cloud 的特点 约定优于配置 开箱即用.快速启动 适用于各种环境 轻量级 ...
- write.table函数语法:
write.table (x, file ="", sep ="", row.names =TRUE, col.names =TRUE, quote ...