POJ 1850 Code

Source: Internet
Author: User

Coordinates make me dizzy. I first typed a table to store the number of solutions with a letter starting with I. Then, it is decomposed from the input string:

Assume that it is a cdvx string, and add the number of all solutions with a length of 1, 2, and 3, and then analyze from the right. The first character c must be at least a, and, the number of solutions starting with B is 4. The minimum length of the second character must be 'C' + 1, that is, d ', so we need '~ The number of schemes whose names start with 'U' is 2 is the same as that of the last one.

Here we should know how to solve the problem. Some people say this is Yang Hui's triangle. It is really Yang Hui's triangle to output the table-making results, but the practice is the same.

Code:

[Cpp]
# Include <iostream>
Using namespace std;
Int dp [30] [30], cnt [30]; // dp is the number of I schemes starting with a character, and cnt is the number of all schemes whose length is I.
Int main ()
{
Int I, j, k, sum, temp;
For (I = 1; I <= 26 & (dp [1] [I] = 1); I ++ );
Cnt [1] = 26;
For (I = 2, sum = 26; I <= 26; I ++ ){
Temp = 0;
For (j = 1; j <= 27-i; j ++ ){
Sum-= dp [I-1] [j];
Dp [I] [j] = sum;
Temp + = sum;
} Www.2cto.com
Sum = temp; cnt [I] = sum;
}

Char str [15];

While (scanf ("% s", str )! = EOF ){
Sum = 0;
Bool flag = true;
Int len = strlen (str );
For (I = 1; I <len; I ++ ){
If (str [I]-str [I-1] <= 0) {// determines if there is any descending order
Flag = false; break ;}
Else sum + = cnt [I];
}
If (flag ){
For (I = 0; I <len; I ++) {// solve the problem by Analysis
J = (I = 0? 1: str [I-1]-'A' + 2 );
For (; j <= str [I]-'A'; j ++)
Sum + = dp [len-I] [j];
}
Printf ("% d \ n", sum + 1 );
}
Else
Printf ("0 \ n ");
}
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.