2014-05-02 03:37

题目链接

原题:

A string is called sstring if it consists of lowercase english letters and no two of its consecutive characters are the same. 

You are given string s of length n. Calculate the number of sstrings of length that are not lexicographically greater than s.
Input format
The only line of input contains the string s. It's length is not greater than 100.
All characters of input are lowercase english letters. Output format:
Print the answer of test modulo to the only line of output. Sample input:
bcd Sample output:

题目:如果一个字符串全部由小写字母组成,并且所有相邻字母都不同,那么这种字符串称为sstring。给定一个字符串,请统计与此字符串长度相同,且字典序不大于该字符串的所有sstring的个数。由于结果可能很大,所以结果对1009取余。

解法:这是典型的数位动态规划题目了。由于相邻字母不能相同,那么dp[i] = dp[i - 1] * (26 - 1)。对于给定的字符串,逐位进行统计和累加即可。要注意给定的字符串有可能不是sstring,也可能是,所以得加以检查。

代码:

 // http://www.careercup.com/question?id=23869663
#include <iostream>
#include <string>
#include <vector>
using namespace std; int countSString(const string &s, const int mod)
{
vector<int> v;
int i, n; n = (int)s.length();
v.resize(n);
v[] = % mod;
for (i = ; i < n; ++i) {
v[i] = v[i - ] * % mod;
} int count = ;
char prev = 'z' + ;
for (i = ; i < n; ++i) {
if (prev >= s[i]) {
count = (count + (s[i] - 'a') * v[n - - i]) % mod;
} else {
count = (count + (s[i] - 'a' - ) * v[n - - i]) % mod;
}
if (prev == s[i]) {
break;
} else {
prev = s[i];
}
} v.clear();
return i == n ? count + : count;
} int main()
{
string s;
const int mod = ; while (cin >> s) {
cout << countSString(s, mod) << endl;
} return ;
}

Careercup - Facebook面试题 - 23869663的更多相关文章

  1. Careercup - Facebook面试题 - 6026101998485504

    2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...

  2. Careercup - Facebook面试题 - 5344154741637120

    2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...

  3. Careercup - Facebook面试题 - 5765850736885760

    2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...

  4. Careercup - Facebook面试题 - 5733320654585856

    2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...

  5. Careercup - Facebook面试题 - 4892713614835712

    2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...

  6. Careercup - Facebook面试题 - 6321181669982208

    2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...

  7. Careercup - Facebook面试题 - 5177378863054848

    2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...

  8. Careercup - Facebook面试题 - 4907555595747328

    2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...

  9. Careercup - Facebook面试题 - 5435439490007040

    2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...

随机推荐

  1. AjaxPro.2使用小结

    这是我最近没事的时候研究的东东,使用AjaxPro.2.dll,从前台调用后台Ajax方法,希望对各位亲有帮助哦.. 1.首先将AjaxPro.2.dll从网上下载下来,打开VS项目,点击项目,右键- ...

  2. HDU 5478 Can you find it(快速幂)

    Problem Description Given a prime number C(1≤C≤2×105), and three integers k1, b1, k2 (1≤k1,k2,b1≤109 ...

  3. JavaScript之表格过滤器

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. 关于柯尼卡美能达bizhub250出现c2557错误解决方法

    打印机出现c2557代码的操作方法 1.         按效用/计数器键 英文是(Utility/Counter) 2.         看到有检查细息(Check Detail) 3.       ...

  5. eBay 开发流程

    1[记录]注册成为eBay开发者(eBay Developers Program)+创建Sanbox Key和Production Key http://www.crifan.com/register ...

  6. 如何让R代码按计划执行

    应用场景:你编写了R代码,每天对提交的数据进行分析,你希望它你吃饭的时候执行完毕,生成图表. 那么你需要安装taskscheduleR的包. 怎么操作,看帮助呗.

  7. tar命令: 对某目录文件打tar包时,排除指定的目录或文件

     如某当前目录存在以下文件或目录: 1.txt2.txt3.txtdir1dir2my2015.tarmy2016.tar 若要对当前目录除1.txt 和dir1.tar外,打包tar 步骤一.建立e ...

  8. Android四大组件之一:BroadCastReceiver(广播接收者)

    广播接受者是(BroadCastReceiver)是Android中的地大组件之一,之前学习了一些关于BroadCastReceiver方面的知识,今天回过头来发现已经快忘记的差不多了,毕竟现在是刚开 ...

  9. 巧用Excel分列功能处理数据

    Technorati 标签: 数据处理      今天,主要工作就是处理测试数据,统计汇总成图表来显示.先来说说要求,然后给出我在折腾这堆数据中遇到的问题以及解决方法.   问题要求:       格 ...

  10. 4.Servlet_Form表单处理

    1.建项目"3Servlet_Form",src下建包“com.amaker.servlet”,web-root下建Register.html <!DOCTYPE html& ...