BOJ 316 password DP

Source: Internet
Author: User

The password question was originally known to use DP, but I don't know how to think about it. Asked the classmate, his general idea is this, assuming the string is Ch [1005], the string length is Len, set DP [0] = 1, 1-> len-1 initialization to 0. DP [I] indicates the total number of combination methods before I bit. Therefore, we require the result of DP [Len].
Example: 1 2 3
Initialize DP: 1 0 0 0
First iteration 1 (1) 1 (12) 1 (123)
Second iteration 2 (1) (2) 2 (1) (23)
Third iteration 2 (12) (3) (1) (2) (3)

The above process can be analyzed as follows:
During the first iteration, 1 <= 255, + dp [0] = 1 at the corresponding position of 2, indicating that 2 can be reached by (1); 12 <= 255, + dp [0] at the position corresponding to 3, indicating that 3 can be reached by (12); 123 <= 255, + dp [0] at the last digit, it indicates that the last digit can be reached through (123. In the second iteration, 2 <= 255, + dp [1] = 1 at the corresponding position of 3, indicating that 3 can be reached from 2; 23 <= 255, + dp [1] at the position corresponding to the last digit indicates that the last digit can be reached through 2. Similarly, we can see that the first iteration divides the data into (1) (12) (123) such a combination (both include 1), the second is divided into (2) (23), and the third is divided into (3.

For cases where 0 is included, such as: 1 0 2
Since 0 must be used as one, or can be combined with the previous one, but cannot be combined with the later one, therefore, you only need to add the corresponding value of 0 to the next bit to jump out (break ).

# Include <stdio. h> # include <stdlib. h> # include <string. h> char ch [1005]; long DP [1005]; long res; int t; int char_to_int (int from, int to) {int ans = 0; for (INT I = from; I <= to; I ++) ans = ans * 10 + (CH [I]-'0'); Return ans ;} int main () {scanf ("% d", & T); While (t --) {memset (DP, 0, sizeof (DP )); DP [0] = 1; scanf ("% s", CH); int Len = strlen (CH); For (INT I = 0; I <Len; I ++) {for (Int J = I; j-I <= 2 & J <Len; j ++) {If (CH [I] = '0 ') {DP [I + 1] + = DP [I]; break; // bounce} else {int TMP = char_to_int (I, j); If (TMP <= 255) DP [J + 1] = (DP [I] + dp [J + 1]) % 1234567; else break; // not satisfied, jump out} printf ("% d \ n", DP [Len] % 1234567);} 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.