Hdu4850 is used to construct a string with a length of N. It is required that any substring with a length of 4 be different.

Source: Internet
Author: User

N "= 50 W. (26 letters are used)

Constructor: 26 strings. A maximum of 26 ^ 4 different strings can be constructed. The maximum length is 26 ^ 4 + 3. Therefore, the output value is "impossble", which is determined by four-dimensional array. Construct one forward character (starting from the last character and then one forward character) each time. In this way, you can create 26 ^ 4-25 types and print them out for discovery (BBBB ~ Zzzz), cannot be constructed, so I learned others' methods, put them at the beginning, and repeat the above method construction (this method can be used to construct them in the future ).

PS: learn from this: if the string S = S + char is used for splicing, the speed is very slow. Use char s [] And then s [size ++] = char, which is much faster. The output is a bit interesting. You can directly output n numbers, starting with the last address.

# Include <iostream> // 46 MS # include <string> # include <cstdio> using namespace STD; int mark [26] [26] [26] [26]; char s [480000]; int size = 0; int main () {int N; For (INT I = 0; I <26; I ++) {s [size ++] = char (I + 'A'); s [size ++] = char (I + 'A '); s [size ++] = char (I + 'A'); s [size ++] = char (I + 'A');} For (INT I = 0; I <size-3; I ++) mark [s [I]-'a'] [s [I + 1]-'a'] [s [I + 2]-'a'] [s [I + 3]-'a'] = 1; int T1 = 25, T2 = 25, T3 = 25; For (INT I = 104; I <= 456979; I ++) {in T count1 = 0; For (Int J = T3 + 1; count1 <2; j ++) {If (j = 26) {J = 0; count1 ++ ;} if (MARK [T1] [T2] [T3] [J] = 0) {mark [T1] [T2] [T3] [J] = 1; char temp = J + 'a'; s [size + +] = temp; T1 = t2; t2 = T3; T3 = J; break ;}}} /* For (INT I = 0; I <26; I ++) for (Int J = 0; j <26; j ++) for (int K = 0; k <26; k ++) for (int t = 0; t <26; t ++) if (MARK [I] [J] [k] [T] = 0) printf ("% d \ n", I, J, K, t); */while (CIN> N) {If (n> 26*26*26*26 + 3) {cout <"impossible" <Endl; c Ontinue;} else {printf ("% s \ n", S + 410479-n); // output n characters! The start address is next to it!} 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.