CareerCup Facebook Number of sstrings

265 篇文章 1 订阅
86 篇文章 0 订阅
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 1009 to the only line of output. 

Sample input: 
bcd 

Sample output: 

653


------------------------------------------------------------------------------------------------

Take notice of the concept of sstring: consists of lowercase English letters and no two of its consecutive characters are the same!!!!!!!!!! 


First, we need to realize that the number of sstrings of size N is 25^N - at each step, we can pick any of the 25 different letters than the last one. 
Then we can apply dynamic programming knowing the current index and the previous letter used.

Say we have "bcd". Pick 'a' for the first letter - since it's smaller than the first letter 'b', we sum the number of sstrings of size 2 (2 remaining characters). Do this for all letters less than the given letter at index i because all subsequent strings are lexicographically smaller. 
After this step, add the number of sstrings with the same first 'i' letters as the given string.


#include <iostream>
#include <map>
#include <algorithm>
#include <limits.h>
#include <assert.h>
using namespace std;
const int MAX = 102, MOD = 1009;
int dp[MAX][27], len, sstrings[MAX];
char str[MAX];

int solve(int i, int prev) {
  if (i == len)
    return dp[i][prev] = 1;
  if (dp[i][prev] == -1) {
    dp[i][prev] = 0;
    for (int c = 'a'; c < (int)str[i]; c++)
      if (c-'a' != prev)
        dp[i][prev] += sstrings[len-i-1];
    if (prev != str[i]-'a')
      dp[i][prev] += solve(i+1, str[i]-'a');
    dp[i][prev] %= MOD;
  }
  return dp[i][prev];
}
int main() {
  scanf("%s", str);
  len = strlen(str);
  sstrings[0] = 1;
  for (int i = 1; i <= len; i++)	// sstrings[i] = 25^i % MOD
    sstrings[i] = (sstrings[i-1] * 25) % MOD;
  memset(dp, -1, sizeof dp);	// -1 meaning not calculated yet
  printf("%d\n", solve(0, 26));	// 26 is a non-existing letter before the start
  return 0;
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值