[Google APAC 2017] Lazy Spelling Bee
2016-07-05 15:00
453 查看
Problem
In the Lazy Spelling Bee, a contestant is given a target word W to spell. The contestant's answer word A is acceptable if it is the same length as the target word, and the i-th letter of A is either thei-th, (i-1)th, or (i+1)th letter of W, for all i in the range of the length of A. (The first letter of A must match either the first or second letter of W, since the 0th letter of W doesn't exist. Similarly, the last letter of A must match either the last
or next-to-last letter of W.) Note that the target word itself is always an acceptable answer word.
You are preparing a Lazy Spelling Bee, and you have been asked to determine, for each target word, how many distinct acceptable answer words there are. Since this number may be very large, please output
it modulo 1000000007 (109 + 7).
Input
The first line of the input gives the number of test cases, T. T test cases follow; each consists of one line with a string consisting only of lowercase English letters(
athrough
z).
Output
For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1) and y is the number of distinct acceptable answer words, modulo 109 + 7.Limits
1 ≤ T ≤ 100.Small dataset
1 ≤ length of each string ≤ 5.Large dataset
1 ≤ length of each string ≤ 1000.Sample
Input | Output |
4 ag aa abcde x | Case #1: 4 Case #2: 1 Case #3: 108 Case #4: 1 |
aa,
ag,
ga,
and
gg.
In sample case #2, the only acceptable answer word is
aa.
题解:多个数相乘取余。(a*b)%c==(a%c)*(b%c)。 大数据要用Long long
#include <bits/stdc++.h>
using namespace std;
int main() {
freopen("A-large-practice.in", "r", stdin);
freopen("A-large.out", "w", stdout);
int t;
scanf("%d",&t);
string s;
for(int cnt=1;cnt<=t;cnt++) {
cin>>s;
int len=s.size();
long long res=1;
for(int i=0;i<len;i++) {
int tmp=1;
if(i>0&&s[i]!=s[i-1]) tmp++;
if(i+1<len&&s[i]!=s[i+1]&&s[i+1]!=s[i-1]) tmp++;
res=(res*tmp);
res%=1000000007;
}
printf("Case #%d: %lld\n",cnt,res%1000000007);
}
return 0;
}
相关文章推荐
- android Google Map获取地理位置信息的方法
- 谷歌 Project Zero 团队宣布新政策,漏洞披露前将有完整的 90 天缓冲期
- Android使用Google Map浅谈
- 专家解读:开源软件项目是否会被限制出口?
- 谷歌正式开始补偿Nexus 6P重启门和电池门用户:最高赔400美元
- 每日安全资讯:谷歌发现 G Suite 漏洞,部分密码明文存储长达十四年
- 专家解读:开源软件项目是否会被限制出口?
- 每日安全资讯:命案侦破过程揭示 Google 能够跟踪全世界的手机
- 书评:《算法之美( Algorithms to Live By )》
- Google排名算法改变 不在以外部链为主
- 动易2006序列号破解算法公布
- Google Adsense 支票托收信息收集 (1-30-2007)
- Google Adsense常用技巧总结
- Google推广的十二点技巧
- google优化圣经做网站必备第1/3页
- Google AdSense英文高价关键词排行列表
- C#递归算法之分而治之策略
- 谷歌、雅虎支持中文域名搜索 有助提升搜索引擎优化
- Ruby实现的矩阵连乘算法