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 ...
随机推荐
- kafka connect简介以及部署
https://blog.csdn.net/u011687037/article/details/57411790 1.什么是kafka connect? 根据官方介绍,Kafka Connect是一 ...
- 线程同步synchronized和ReentrantLock
一.线程同步问题的产生及解决方案 问题的产生: Java允许多线程并发控制,当多个线程同时操作一个可共享的资源变量时(如数据的增删改查),将会导致数据不准确,相互之间产生冲突. 如下例:假设有一个卖票 ...
- 转移灶,原发灶,cfDNA的外显子测序得到的突变点的关系
文章名称:Exome Sequencing of Cell-Free DNA from Metastatic Cancer Patients IdentifiesClinically Actionab ...
- ucsc genome brower的用法和说明(一)
官网说明书:http://genome.ucsc.edu/goldenpath/help/hgTracksHelp.html 1.genome brower的作用 a,展示任何尺度的基因组片段.比如, ...
- MongoDB快速入门(三)- 集合
创建集合 MongoDB 的 db.createCollection(name, options) 用于创建集合. 在命令中, name 是要创建集合的名称. Options 是一个文档,用于指定集合 ...
- 一键安装 lnmp/lamp/lanmp
1.使用putty或类似的SSH工具登陆VPS或服务器 # screen -S lnmp 如果提示screen: command not found 命令不存在可以执行:yum install scr ...
- Centos6.5下Hbase安装
下载 http://mirror.bit.edu.cn/apache/hbase/hbase-0.94.26/hbase-0.94.26.tar.gz 2. 解压 tar -zxvf hbase-0 ...
- apache基于端口的虚拟主机配置
主机ip: 192.168.7.51 Centos6.5 三个目录/usr/ftp/test/usr/ftp/dev/usr/ftp/demo 实现效果192.168.7.51:8052访问/usr/ ...
- SEM竞价数据基本分析方法
今天我们从账户数据表现来看一看怎样通过数据分析,判断账户出现的问题及解决思路.也欢迎大家提出意见,共同讨论进步. 首先我们从关键词报告来分析数据: 以上图数据为例.(设定该行业CPC均价为8) 先说下 ...
- 有些 where 条件会导致索引无效
在查询中,WHERE 条件也是一个比较重要的因素,尽量少并且是合理的 where条件是徆重要的,尽量在多个条件的时候,把会提取尽量少数据量的条件放在前面,减少后一个 where 条件的查询时间.有些 ...