UV 417-Word Index (Digital dp)

Source: Internet
Author: User

Question connection: Ultraviolet A 417-Word Index

According to the requirements in the question, it is a string number. Now we will give a string and ask how many strings are numbered. Note that the string must be incrementing; otherwise, the number is 0.

Solution: calculate the number of strings that are smaller than the given string and meet the increasing requirement. Dp [I] [j] indicates a string whose I-th position is j that is smaller than the given string and must be incremented.
Dp [I] [j] = Σ k = 0j? 1dp [I? 1] [k].
Pay attention to the situation of processing the boundary each time, and add its own strings at last. When processing the boundary, dp [I] [0] must be assigned a value of 1, which indicates that the first I is null.

#include 
  
   #include 
   
    #include using namespace std;const int N = 10;char str[N];int dp[N][3*N];int solve () {    int len = strlen(str);    if (len == 1)        return str[0] - 'a' + 1;;    for (int i = 1; i < len; i++)        if (str[i] <= str[i-1])            return 0;    int pre = str[0] - 'a' + 2;    memset(dp, 0, sizeof(dp));    for (int i = 0; i + 'a' <= str[0]; i++)        dp[1][i] = 1;    for (int i = 1; i < len; i++) {        for (int j = 0; j <= 26; j++) {            for (int k = j+1; k <= 26; k++)                dp[i+1][k] += dp[i][j];        }        for (int j = pre; j + 'a' <= str[i]; j++)            dp[i+1][j]++;        pre = str[i] - 'a' + 2;        dp[i+1][0]++;    }    int ans = 1;    for (int i = 1; i <= 26; i++)        ans += dp[len][i];    return ans;}int main () {    while (scanf("%s", str) == 1) {        printf("%d\n", solve());    }    return 0;}
   
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.