Cf282 e sausage maximization [trie tree]

Source: Internet
Author: User

N count

Returns the maximum value of an exclusive or prefix (in the past, all numbers are taken as different or) and an exclusive or suffix (from the end, all numbers are taken as different or ).

Good pitfalls, good pointers

First trie tree

Simply put, the solution (in fact, the shell is still not deep ):

Use an exclusive number or all numbers as the initial suffix

Then the numbers from the past and the future are listed one by one from the suffix and entered the prefix,

In this process, the current prefix is changed to binary and pushed to trie. Then, the current suffix is changed to binary from high to low. Try to get the value different from its digit and go down along trie, get the best number and maintain the maximum value different from the suffix or

Pointer is a pitfall.

In fact, I am still a bit confused.

Let's talk about the pitfalls.

At the beginning, there were no 64-digit full storage (as a result, a "Optimal" prefix with a length of 30 suffix may be 6 or something)

Then, when I judge each digit, it is like Num & (1 <I). Obviously, it is wrong. It should be (Num> I) & 1.

Also, we should press the prefix into trie, But I press number [I] into trie.

And... 1 <55 is wrong, it should be 1ll <55

# Include <cstdio> # include <cstring> # include <cstdlib> # include <algorithm> # include <iostream> using namespace STD; # define max 5 typedef struct trie {trie * Next [Max]; long V; // changed as needed}; trie root; void createtrie (char * Str) {long Len = strlen (STR); trie * P = & root, * q; For (long I = 0; I <Len; ++ I) {long id = STR [I]-'0'; // If (p-> next [ID] = NULL) {q = (trie *) malloc (sizeof (trie); q-> V = 1; // initial v = 1for (long J = 0; j <Max; ++ J) q-> next [J] = NULL; p-> next [ID] = Q; P = p-> next [ID];} else {P-> next [ID]-> V ++; P = p-> next [ID] ;}} p-> V =-1; // if it is the end, change V to-1, indicating} long findtrie (char * Str) {long Len = strlen (STR); trie * P = & root; for (long I = 0; I <Len; ++ I) {long id = STR [I]-'0 '; // change p = p-> next [ID] here as needed; If (P = NULL) // if it is an empty set, return 0 is not saved as the prefix; if (p-> V =-1) // In the character set, the existing string is the prefix of this string. Return-1 ;} Return-1; // the prefix of a string in the character set} long dealtrie (trie * t, bool isroot) {long I; If (t = NULL) return 0; for (I = 0; I <Max; I ++) {If (t-> next [I]! = NULL) dealtrie (t-> next [I], false);} If (! Isroot) Free (t); Return 0;} const long nn = 111111; long f [NN]; # define save 62 void get_bina (long s, char *) {long que = 0; For (long I = save; I> = 0; I --) {to [Que ++] = (S> I) & 1) + '0'; // It should be (S> I) & 1} to [Que] = '\ 0';} int main () {# ifndef online_judgefreopen ("/home/raw.96/in.txt", "r", stdin); # endiflong long n; CIN> N; For (long I = 1; I <= N; I ++) {CIN> F [I];} long prefix = 0, Postfix = 0, ANS = 0; for (long I = 1; I <= N; I ++) Postfix ^ = f [I]; // The initial suffix ans = Postfix; // ans initially obtains all suffixes for (long I = 1; I <= N; I ++) {Postfix ^ = f [I]; prefix ^ = f [I]; char TMP [99]; get_bina (prefix, TMP); // convert the current prefix to binary createtrie (TMP ); // press the current prefix into trieget_bina (Postfix, TMP); // program the current Suffix in binary trie * P = & root; long Len = strlen (TMP ); long long get_num = 0; ans = max (ANS, postfix); // do not get the prefix for (long I = 0; I <= save; I ++) {// obtain long want = 1-(TMP [I]-'0') one by one; // The optimal match if (p-> next [want] = NULL) {// if there is no optimal match if (p-> next [1-want] = NULL) {// if none, it means it ends with ans = max (ANS, get_num ^ postfix); break;} else {// unmatched P = p-> next [1-want]; get_num = get_num * 2 + 1-want ;}} else {// has the optimal match P = p-> next [want]; get_num = get_num * 2 + want ;}} ans = max (ANS, get_num ^ postfix );} cout <ans <Endl; // dealtrie (& root, true); Return 0 ;}

Cf282 e sausage maximization [trie tree]

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: 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.