Practice Round APAC test 2017——1.Problem A. Lazy Spelling Bee

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, TT 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


Input 
 

Output 
 
4
ag
aa
abcde
x

Case #1: 4
Case #2: 1
Case #3: 108
Case #4: 1

In sample case #1, the acceptable answer words are aaagga, 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;
}


阅读更多
换一批

Spelling Be

12-16

DescriptionnnIt's a simple requirement your company has, really — every document should be spell-checked before it's sent out to a customer. Unfortunately, while word processing documents are easily spell-checked, your employees have not been checking email every time they send out a message. So you've come up with a little improvement. You are going to write a program that will check email on its way out. You will spell-check each message, and if you find any spelling errors, it will be returned to the sender for correction. nnWhen you announced this plan, one of your coworkers fell off their chair laughing, saying that you couldn't possibly anticipate every name, technical acronym, and other terms that might appear in an email. Undaunted, however, you are going to test-run your code with an online dictionary and some sample emails you have collected. nInputnnThe input consists of two sections, the dictionary and the emails. The first line of input specifies the number of words in the dictionary, followed by that many lines, with one word per line. There is no whitespace before, after, or in any words,although there may be apostrophes or hyphens in the words, which are considered part of the word (i.e. "bobs" is different than "bob's". There will be no duplicate words. All words will be in lower case. nnFollowing that are the emails. The first line of this section has the number of emails in the input. Following that line begins the first email. It has been preprocessed, so it consists of one word per line, with no punctuation (other than apostrophes and hyphens) or whitespace, and all words are in lower case. The last word in the email is followed by a line with only "-1". Each email will have at least one word.nOutputnnFor each email, you must either print: nnEmail X is spelled correctly. nnwhere Xbegins with 1 and counts up. Or, if a word is found that is not in the dictionary, print out: nnEmail X is not spelled correctly. nnfollowed by a list of unknown words in the order that you find them, one per line. If an unknown word is found multiple times, it should be printed multiple times. nnThere are no spaces between datasets. Following the output for the final dataset, print a line stating "End of Output". nSample Inputnn6nalicenamnbitterlyninleavingnyoun1ndearnbobninamnleavingnyounbitterlynalicen-1nSample OutputnnEmail 1 is not spelled correctly.ndearnbobnEnd of Output

没有更多推荐了,返回首页