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. 【.NET基础】--委托、事件、线程(1)

    1,委托 是存放方法的指针的清单,也就是装方法的容器 A, 新建winform项目[01委托],项目中添加dg_SayHi.cs 委托类 用于存储方法 namespace _01委托 { //定义委托 ...

  2. 十三、Android学习笔记_Andorid控件样式汇总

    <!-- 设置activity为透明 --> <style name="translucent"> <item name="android: ...

  3. SQL里IN的用法以及优化

    1.in后条件不多,可以考虑主表建索引,或用union all 代替 2. in 和 exists的区别: 如果子查询得出的结果集记录较少,主查询中的表较大且又有索引时应该用in, 反之如果外层的主查 ...

  4. SQL Server 2008R2 禁用远程连接

    很多人在开发过程中都会用多数据库(这里仅讨论MSSQL),也都会在服务器上装MSSQL,在你装上MSSQL后,机器上的1433端口就被激活了.如果你的服务器是在内网,也许不用过多的关注,如果你的服务器 ...

  5. 20150515--关于IIS的备忘(WIN7)

    一.IIS服务位置: 1)控制面板--程序和功能 2)点击打开或关闭Windows功能, 3)Internet服务信息(英文:internet information services)--Web管理 ...

  6. 洛谷 P1209 修理牛棚== Codevs 2079 修理牛棚

    时间限制: 1 s   空间限制: 128000 KB   题目等级 : 黄金 Gold 题目描述 Description 在一个夜黑风高,下着暴风雨的夜晚,farmer John的牛棚的屋顶.门被吹 ...

  7. MVC校验

    首先Model里面需要写好校验标签, 我的数据库中有个tblUserInfo表,其中有Id,UserName,,Age三个列,Id自动增长 Model添加UserInfo Class,在UserNam ...

  8. ADO.NET笔记——使用Command执行增删改操作,通过判断ExecuteNonQuery()返回值检查是否操作成功

    相关知识: ExecuteNonQuery()方法:执行CommandText属性所制定的操作,返回受影响的记录条数.该方法一般用来执行SQL中的UPDATE.INSERT和DELETE等操作 对于U ...

  9. 正则匹配 去掉 多余的js和html标签

    $reg17 = '/><strong>公司介绍<\/strong><\/td>([\S\s*]+?)<\/div>/'; $this->d ...

  10. 封装cookie

    function cookie(name,value,expires){ switch(typeof value){ case 'string': //设置 var exp=''; if(expire ...