"USACO3.1.5" contact-bit operation/KMP/AC Automatic machine

Source: Internet
Author: User
Tags int size pack stdin

Spit Groove: The output of this problem is disgusting, disgusting, disgusting, disgusting, *10000 ...


Tle Method:

Violent KMP, put all the 100 101 111-like schemes, all and the original string match once ... Then sort the output.

compiling ... Compile:ok

Executing
   ... Test 1:test OK [0.008 secs, 9424 KB]
   test 2:test OK [0.003 secs, 9424 KB]
   test 3:test OK [0.032 secs, 9424 kb]< C5/>test 4:test OK [0.065 secs, 9424 KB]
  > Run 5:execution error:your Program ("contact") used more than the
        allotted runtime of 1 seconds (it ended or WA S stopped at
        1.674 seconds) while presented with test Case 5. It used 9420 KB of
        
/* task:contact lang:c++ */#include <iostream> #include <algorithm> #include <cstdio> #include <cst

Dlib> #include <cstring> using namespace std;
BOOL text[200020];
int L, R, N, tlen=0, Slen, next[50];

BOOL SON[50];
	void Init () {scanf ("%d%d%d\n", &l, &r, &n);
	Char ch;
		while (1) {ch = GetChar ();
		if (ch = = ' \ n ') continue;
		if (ch = = EOF) break;
	text[tlen++] = ch-' 0 ';
	}} void cal (int k, int w)//k this number, converted to w digit {slen = w-1;
	memset (son,0,sizeof (son));	
		while (k) {son[slen--] = k% 2;
	K/= 2;
} slen = W;
	} int KMP (bool *son, bool *text, int slen, int tlen) {int i = 0, j =-1;
	int ans = 0;
	Next[0] =-1;	
		while (i! = Slen) {if (j = =-1 | | son[i] = = Son[j]) Next[++i] = + + J;
	else J = Next[j];
	} i = 0, j = 0;	
		while (i! = Tlen) {if (j = =-1 | | text[i] = = Son[j]) ++i, + + j;
		else J = Next[j];
			if (j = = Slen) {+ + ans;
		j = Next[j];
}} return ans;
	} struct Pack {int key;
int k, W; }a[500000];

int tail=0; BOOL Operator < (pack A, pack B) {if (A.key! = B.key) return a.key > B.key;//First is the number of repetitions if (a.w! = B.W) return A.
	W < B.W;
if (A.K! = B.K) return A.K < B.K; } void doit () {for (int i = L; I <= R; + + i)//exhaustive position {for (int j = 0; J <= (1 << i)-1; + + j)//poor-lift decimal number, but
			Replace with binary {cal (J, I);//A[tail].key = KMP (son, text, Slen, Tlen);
			if (A[tail].key = = 0) continue;
			A[TAIL].K = j;
			A[TAIL].W = i;
		++tail;
	}} sort (A, a + tail); int tmp = -1,t=0, now=0;
	The last occurrence of the number of cycles, the output of how many different loops, the current node int output_num;
			while (now! = tail) {if (A[now].key! = tmp)//New repeat number {if (t = = n) break;
			if (tmp! =-1) printf ("\ n");
			printf ("%d", tmp = A[now].key);
			Putchar (' \ n ');	
			+ + t;
		Output_num = 0;
			}else{if (output_num! = 6) Putchar (");
				else {putchar (' \ n ');
			Output_num = 0;
		}} cal (A[NOW].K, A[NOW].W);
		for (int i = 0; I! = A[NOW].W; + + i) Putchar (son[i]+ ' 0 ');
		+ + Output_num;
	++now;
	}Putchar (' \ n ');
	} int main () {freopen ("contact.in", "R", stdin);
	Freopen ("Contact.out", "w", stdout);
	Init ();
	Doit ();
return 0;
 }





Ac Method 1:

Bit operations. Think of the original string as a binary string. Put the back 1 bits, 2 bits, 3 bits ... are intercepted, converted to decimal k, and then to B[k][i] (decimal is k, binary is I bit), save into B array, and then the B array to sort output.


Point 1:0 00 000 is not the same string, so you can record a number in the end is a few.

Point 2: For the sake of trouble, in the addition of CMP information to sort, you can tamper with the 3 keywords, to achieve a sort of direct output, no need to make any adjustments. (First keyword positive sequence, second third keyword reverse)


Very good speed.

   executing ... Test 1:test OK [0.003 secs, 9816 KB]
   test 2:test OK [0.000 secs, 9816 KB]
   test 3:test OK [0.003 secs, 9816 kb]< C3/>test 4:test OK [0.003 secs, 9816 KB]
   test 5:test OK [0.005 secs, 9816 KB]
   test 6:test OK [0.019 secs, 9816 KB]
   Test 7:test OK [0.014 secs, 9816 KB] all

tests OK.
/* task:contact lang:c++ */#include <iostream> #include <algorithm> #include <cstdio> #include <cst

Dlib> #include <cstring> using namespace std;
int L, R, N, Slen, tlen=0;

int son[13];
	The inline void cal (int k, int w)//k this number into the W-digit {slen = w-1;
	memset (son,0,sizeof (son));	
		while (k) {son[slen--] = k% 2;
	K/= 2;
} slen = W;  } struct Pack {int key; Number of occurrences int k, W;
The simulation is k, the number of bits is w}a[140000];

int tail=0;

int b[9000][15]={0}; inline bool operator < (pack A, pack B) {if (A.key! = B.key) return a.key > B.key;//First is the number of repetitions if (a.w! = B.W) re
	Turn A.W < B.W;
if (A.K! = B.K) return A.K < B.K; } void doit () {for (int i = L; I <= R; + + i) {for (int j = 0; J <= (1 << i)-1; + + j) {if (B[j]
			[i] = = 0) continue;
			A[tail].key = B[j][i];
			A[TAIL].W = i;
		A[TAIL++].K = j;
	}} sort (A, a + tail); int tmp = -1,t=0, now=0;
	The last occurrence of the number of cycles, the output of how many different loops, the current node int output_num; while (now! = tail) {iF (a[now].key! = tmp)//new repetition number {if (t = = n) break;
			if (tmp! =-1) printf ("\ n");
			printf ("%d", tmp = A[now].key);
			Putchar (' \ n ');	
			+ + t;
		Output_num = 0;
			}else{if (output_num! = 6) Putchar (");
				else {putchar (' \ n ');
			Output_num = 0;
		}} cal (A[NOW].K, A[NOW].W);
		for (int i = 0; I! = A[NOW].W; + + i) Putchar (son[i]+ ' 0 ');
		+ + Output_num;
	++now;
} putchar (' \ n ');
	} inline void Check (int tot, int wei)//tot this number to detect all Wei bits (binary) of the number {int s = (1 << wei); int t = tot & ((1 << wei)-1);
Tot xxx B[t][wei] + +;
	} int main () {freopen ("contact.in", "R", stdin);
	Freopen ("Contact.out", "w", stdout);
	scanf ("%d%d%d\n", &l, &r, &n);
	Char ch;
	int tot = 0;
		while (1) {ch = GetChar ();
		if (ch = = ' \ n ') continue;
		if (ch = = EOF) break;
		++tlen;	
		Tot <<= 1; Tot + = ch== ' 0 '? 0:1;	The new number is inserted for (int i = L; I <= min (R, tlen); + + i) check (tot, i);
	Tot length is i} doit ();
return 0;
 }

Method 3:ac Automatic Machine


Because the AC automaton has multiple matching functions, so that the length of the binary string in the range is put into the trie, and then directly put the subsequent string in the AC automaton run on the line ~ speed is very fast.   2 programs at the speed of the same ... Method 4, I really can't think of ... Other programs are slower than me, I guess that's the difference between GetChar.


compiling ... Compile:ok

Executing
   ... Test 1:test OK [0.008 secs, 5676 KB]
   test 2:test OK [0.008 secs, 5676 KB]
   test 3:test OK [0.011 secs, 5932 kb]< C5/>test 4:test OK [0.008 secs, 5676 KB]
   test 5:test OK [0.024 secs, 5932 KB]
   test 6:test OK [0.032 secs, 5676 KB]
   Test 7:test OK [0.032 secs, 5932 KB] all

tests OK.
/* task:contact lang:c++ */#include <iostream> #include <algorithm> #include <cstdio> #include <cst

dlib> #include <queue> #include <cstring> using namespace std;
int L, R, N, Slen, tlen=0;

int son[13];
	The inline void cal (int k, int w)//k this number into the W-digit {slen = w-1;
	memset (son,0,sizeof (son));	
		while (k) {son[slen--] = k% 2;
	K/= 2;
} slen = W;  } struct Pack {int key; Number of occurrences int k, W;
The simulation is k, the number of bits is w}a[140000];

int tail=0;

int b[9000][15]={0}; inline bool operator < (pack A, pack B) {if (A.key! = B.key) return a.key > B.key;//First is the number of repetitions if (a.w! = B.W) re
	Turn A.W < B.W;
if (A.K! = B.K) return A.K < B.K; } void doit () {for (int i = L; I <= R; + + i)//Sort {for (int j = 0; J <= (1 << i)-1; + + j) {if
			(B[j][i] = = 0) continue;
			A[tail].key = B[j][i];
			A[TAIL].W = i;
		A[TAIL++].K = j;
	}} sort (A, a + tail); 
	int tmp = -1,t=0, now=0;
	int output_num; while (now! = tail)//nausea Output {if (A[now].key! = tmp)
			{if (t = = n) break;
			if (tmp! =-1) printf ("\ n");
			printf ("%d", tmp = A[now].key);
			Putchar (' \ n ');	
			+ + t;
		Output_num = 0;
			}else{if (output_num! = 6) Putchar (");
				else {putchar (' \ n ');
			Output_num = 0;
		}} cal (A[NOW].K, A[NOW].W);
		for (int i = 0; I! = A[NOW].W; + + i) Putchar (son[i]+ ' 0 ');
		+ + Output_num;
	++now;
} putchar (' \ n ');
	} struct Node {int flag;
	int k, W;
	Node *fail;
	Node *c[2];
		Node () {flag = k = w = 0;
		Fail = NULL;
	C[0] = c[1] = NULL;


}
};
	struct AC_DFA {int size;
	queue<node*>q;

	Node *root;	
	Ac_dfa () {root = new node;
		} inline void ins (int *word, int len, int k) {node *now = root;
			for (int i = 0; I! = Len; + + i) {if (!now-c[word[i]]) Now, c[word[i]] = new node;
		now = C[word[i]]; } now-flag = 1; Here are the words now-k = k;
	Now, w = Len;
		} void Setfail () {Q.push (root);
		Root-fail = NULL; while (!q. empty ()) {Node *now = Q.front ();
			Q.pop ();	
					for (int i = 0; i = 2; + + i) {if (now-c[i]) {node *p = now-fail;
					while (P &&!p-c[i]) p = P-fail; Now-and c[i]-fail = p?
					P--c[i]: root;
				Q.push (now--c[i]); }else now-c[i] = now = = root?
			Root:now, Fail-and c[i];

}}}}ac;
	void Init () {scanf ("%d%d%d\n", &l, &r, &n);	
			for (int i = L; I <= R; + + i) {for (int j = 0; J! = (1 << i); + + j) {cal (J, I);
		Ac.ins (son, I, j);
}} ac.setfail ();
	} int main () {freopen ("contact.in", "R", stdin);
	Freopen ("Contact.out", "w", stdout);
	Init ();
	Char ch;
	Node *now = Ac.root, *tmp;
		while (1) {ch = GetChar ();
		if (ch = = ' \ n ') continue;
		if (ch = = EOF) break;
		now = c[ch-' 0 '];
		TMP = Now;
			while (TMP && TMP, flag) {b[tmp, k][tmp, W] + +;	
		TMP = TMP, fail;
	}} doit ();
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.