Careercup - Facebook面试题 - 23869663
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的更多相关文章
- Careercup - Facebook面试题 - 6026101998485504
2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...
- Careercup - Facebook面试题 - 5344154741637120
2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...
- Careercup - Facebook面试题 - 5765850736885760
2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...
- Careercup - Facebook面试题 - 5733320654585856
2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...
- Careercup - Facebook面试题 - 4892713614835712
2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...
- Careercup - Facebook面试题 - 6321181669982208
2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...
- Careercup - Facebook面试题 - 5177378863054848
2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...
- Careercup - Facebook面试题 - 4907555595747328
2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...
- Careercup - Facebook面试题 - 5435439490007040
2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...
随机推荐
- 百度编辑器UEditor,地址栏传值长度有限-在webConfig配置
<system.web> <httpRuntime requestValidationMode=" ></httpRuntime> <compila ...
- Javascript 日期时间超强正则表达式
var reg = /^([0-9]{4})-((?:0[1-9]|[1-9]|1[1-2]))-((?:(?:0[1-9]|[1-9])|1[0-9]|2[0-9]|3[0-1]))$|^([0-9 ...
- .NET使用NPOI读取Word模板并替换关键字并下载
NPOI 是 POI 项目的 .NET 版本.POI是一个开源的Java读写Excel.WORD等微软OLE2组件文档的项目. 使用 NPOI 你就可以在没有安装 Office 或者相应环境的机器上对 ...
- WIN7系统操作快捷键
轻松访问 按住右Shift八秒钟:启用或关闭筛选键 按左 Alt+左 Shift+PrntScrn(或 PrntScrn):启用或关闭高对比度 按左 Alt+左 Shift+Num Lock :启用或 ...
- OSPF多区域配置
1.配置三台路由器IP R1(config)#INTER S1/0 R1(config-if)#IP ADDress 192.1.12.1 255.255.255.0 R1(config-if)#no ...
- 第七篇、OC_图片的裁剪基于SDWebImage
前期有段时间困扰了我很久一个问题由于工程中的图片数据抓取自不同平台,所以图片的大小尺寸不一定,而放置图片的imageView尺寸是一定的,不作任何处理的话会导致图片拉伸变形,因此找了好久解决办法,现把 ...
- iOS - 移动设备防丢失App
一.原理 二.数据获取 三.报警
- PHP调用WebService
1. 环境配置 配置php.ini,把php_soap.dll前面的分号去掉, 配置完成,需要重启. 2. PHP调用代码,如下 <?php try { $soap = new SoapCl ...
- python:笔记for循环中的else
我们常常会在for循环遍历一个序列或者字典后,接着语句else,很多新手会误以为是判断执行else后面的 代码,其实不然,for循环里面也不存在判断,而已执行完遍历的对象后,再执行else后面的代码. ...
- mysql之触发器before和after的区别(2)
我们先做个测试: 接上篇日志建的商品表g和订单表o和触发器 假设:假设商品表有商品1,数量是10: 我们往订单表插入一条记录: insert into o(gid,much) values(1,20) ...