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

Input
1
ab
Output
1
Input
1
aaaaaaaaaaa
Output
0
Input
2
ya
klmbfxzb
Output
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)的更多相关文章

  1. [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆)

    [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆) 题面 一棵二叉树的所有点的点权都是给定的集合中的一个数. 让你求出1到m中所有权 ...

  2. codeforces 721C (拓排 + DP)

    题目链接:http://codeforces.com/contest/721/problem/C 题意:从1走到n,问在时间T内最多经过多少个点,按路径顺序输出. 思路:比赛的时候只想到拓排然后就不知 ...

  3. codeforces 711C Coloring Trees(DP)

    题目链接:http://codeforces.com/problemset/problem/711/C O(n^4)的复杂度,以为会超时的 思路:dp[i][j][k]表示第i棵数用颜色k涂完后bea ...

  4. codeforces 55D - Beautiful numbers(数位DP+离散化)

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  5. Codeforces 543D. Road Improvement (树dp + 乘法逆元)

    题目链接:http://codeforces.com/contest/543/problem/D 给你一棵树,初始所有的边都是坏的,要你修复若干边.指定一个root,所有的点到root最多只有一个坏边 ...

  6. Codeforces 467C. George and Job (dp)

    题目链接:http://codeforces.com/contest/467/problem/C 求k个不重叠长m的连续子序列的最大和. dp[i][j]表示第i个数的位置个序列的最大和. 前缀和一下 ...

  7. Codeforces 711 C. Coloring Trees (dp)

    题目链接:http://codeforces.com/problemset/problem/711/C 给你n棵树,m种颜色,k是指定最后的完美值.接下来一行n个数 表示1~n树原本的颜色,0的话就是 ...

  8. Codeforces 706 C. Hard problem (dp)

    题目链接:http://codeforces.com/problemset/problem/706/C 给你n个字符串,可以反转任意一个字符串,反转每个字符串都有其对应的花费ci. 经过操作后是否能满 ...

  9. CodeForces 163A Substring and Subsequence dp

    A. Substring and Subsequence 题目连接: http://codeforces.com/contest/163/problem/A Description One day P ...

随机推荐

  1. 前端基础之JavaScript_(5)_DOM对象总结

    DOM 定义了访问和操作HTML文档的标准方法 访问(查找标签) //---- 直接查找 document.getElementById(“idname”) document.getElementsB ...

  2. mapInfo文件格式详解

    from:http://hotolee.blog.163.com/blog/static/3815229920098434956370 MapInfo以表(Tab)的形式存储信息,每个表是由一组Map ...

  3. Serv-u 外网访问内网的FTP服务器

    1. 背景简介 最近研究如何在内网搭架FTP服务器,同时要保证外网(公网)能访问的到.终成正果,但走了一些弯路,在此记下,以飨后人. 2. 基础知识 FTP 使用 2 个端口,一个数据端口和一个命令端 ...

  4. 020_自己编写的wordcount程序在hadoop上面运行,不使用插件hadoop-eclipse-plugin-1.2.1.jar

    1.Eclipse中无插件运行MP程序 1)在Eclipse中编写MapReduce程序 2)打包成jar包 3)使用FTP工具,上传jar到hadoop 集群环境 4)运行 2.具体步骤 说明:该程 ...

  5. [原创]spring及springmvc精简版--AOP

    接上一篇:[原创]spring及springmvc精简版--IOC 理解AOP.java是一种面向对象的语言.而AOP是面向切面,在我看来是面向逻辑或者业务编程,它是对一组逻辑的抽象和分配. 经典例子 ...

  6. iOS_核心动画CALayer(一)

    目 录: 一.核心动画简介 二.图层与视图之间的关系 三.CALayer的使用说明 四.CALayer的隐式动画属性 五.在CALayer上绘图 六.总结     一.核心动画简介 Core Anim ...

  7. Android LCD(二):常用接口原理篇【转】

    本文转载自:http://blog.csdn.net/xubin341719/article/details/9125799 关键词:Android LCD TFT TTL(RGB)  LVDS  E ...

  8. JMeter学习(一)目录介绍

    JMeter也学了一阵子了,对于基本的操作已了解,再回过头来看看Jmeter的目录,本篇是对于它的目录进行一些简单的介绍. JMeter解压之后打开,根目录如下图: 1.bin:可执行文件目录 2.d ...

  9. ubuntu中如何添加IP

    编辑网卡配置文件vi /etc/network/interfaces 在配置文件下增加新的IP配置 之后重启网络/etc/init.d/networking restart

  10. spark学习4(zookeeper3.4集群搭建)

    第一步:zookeeper安装 通过WinSCP软件将zookeeper-3.4.8.tar.gz软件传送到/usr/zookeeper/目录下 [root@spark1 zookeeper]# ch ...