1.Problem A. Lazy Spelling Bee
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 the i-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 (a
through 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
In sample case #1, the acceptable answer words are aa
, ag
, ga
, and gg
.
In sample case #2, the only acceptable answer word is aa
.
首字母和尾字母需要特殊处理
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <stdio.h>
using namespace std;
const int INF = 1e9+7;
int main()
{
//freopen("/Users/fengguowen/Downloads/A-large-practice.in","r",stdin);
//freopen("/Users/fengguowen/Downloads/A-large-practice2.out","w",stdout);
int T;
string s;
int cnt = 1;
int i;
cin>>T;
while(T--)
{
cin>>s;
long long ans = 1;
int n=s.size();
if(n==1)
ans = 1;
else if(n==2)
{
if(s[0]!=s[1])
ans = 4;
else
ans = 1;
}
else
{
if(s[0]!=s[1])
ans=2;
for(i=1;i<n-1;i++)
{
int t=1;
if(s[i]!=s[i-1])
t++;
if(s[i+1]!=s[i]&&s[i+1]!=s[i-1])
t++;
ans = (ans*t)%INF;
}
if(s[n-2]!=s[n-1])
ans = (ans*2)%INF;
}
cout<<"Case #"<<cnt++<<": "<<ans<<endl;
}
return 0;
}