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 ...
随机推荐
- 【网络收集】如何修改vs tfs的登录名和密码 .
连接TFS时,如果本机保存了用户的网络密码,不会出现用户名和密码的输入框,若要更换TFS的用户名和密码,需按以下步骤操作:控制面板--->用户账号--->管理网络密码,此时会列出所有保存了 ...
- Android之聊天室设计与开发
我们要设计和实现一个有聊天室功能的APP,需要服务器不断读取来自客户端的信息,并即时地将信息发送给每个连接到本服务器上的客户端,每个客户端可以向服务器发送消息,并不断地接收来自服务器的消息,并将消息显 ...
- MVC 全站开启缓存,缓解服务器的请求压力
protected void Application_BeginRequest() { //获取当前请求的url string url = HttpContext.Current.Request.Pa ...
- 20150226—C# winform中的ListView解析
ListView在WinForm中多用于表的构建,可以直观的显示表的信息,其格式如同SQL的表 这是他的位置,在公共控件中: Listview的几个重要属性:Columms(集合).Groups(集合 ...
- 银联接口测试——详细(JAVA)
准备材料 1.注册账号 https://open.unionpay.com/ajweb/register?locale=zh_CN 2.▼登录账号 -->帮助中心--> 下载,选择网关支付 ...
- 南阳理工oj88--汉诺塔(一)
题目链接.http://acm.nyist.net/JudgeOnline/problem.php?pid=88 #include <stdio.h> /* //测试一下49999和500 ...
- DailyWallpaper v1.02 released
上次忘了写软件说明,先补上一个. 软件说明: 每天定时(暂定上午11点)下载美国国家地理网站的photo of the day图片作为桌面壁纸.下载图片会以日期为名称保存在C:\DailyWallpa ...
- python 中range与xrange的区别
先来看看range与xrange的用法介绍 help(range)Help on built-in function range in module __builtin__: range(...) r ...
- 【风马一族_Java】在某个范围内,找出具有水仙花特征的数字
打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身.例如: 153是一个"水仙花数",因为153=1的三 ...
- Ubantu 安装 LAMP环境
1.通过 apt-get update 命令从ubantu软件源中下载可安装软件的列表. 2.通过一条命令可以一次性的安装LAMP apt-get install apache2 php5 mysql ...