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;
}