Lost's revenge hdu3341

Source: Internet
Author: User

AC automation + state compression DP, because the time limit card is relatively dead, the selection of the advanced and low levels may lead to TLE (O (∩ □╰) O). For details, see the notes.

# Include <iostream> # include <cstdio> # include <cstdlib> # include <cmath> # include <queue> # include <algorithm> # include <vector> # include <cstring> # include <stack> # include <cctype> # include <utility> # include <map> # include <string> # include <climits> # include <set> # include <string> # include <sstream> # include <utility> # include <ctime> Using STD:: priority_queue; Using STD: vector; Using STD: swap; Using STD: S Tack; Using STD: sort; Using STD: Max; Using STD: min; Using STD: pair; Using STD: Map; Using STD: string; using STD: CIN; Using STD: cout; Using STD: Set; Using STD: queue; Using STD: string; Using STD: istringstream; Using STD :: make_pair; Using STD: Getline; Using STD: greater; Using STD: Endl; Using STD: multimap; typedef long ll; typedef unsigned long ull; typedef pair <int, int> pair; typedef multimap <in T, int> MMAP; const int maxn (510); const int sigma_size (4); const int maxm (110); const int MaxE (4000010 ); const int maxh (18); const int INFI (INT_MAX-1)> 2); const int Mod (20090717); const ull Lim (limit 000ull); int MP [128]; struct AC {int ch [maxn] [sigma_size]; int Val [maxn], F [maxn]; int size; void Init () {memset (CH [0], 0, sizeof (CH [0]); F [0] = Val [0] = 0; size = 1;} inline int idx (char temp ){ Return MP [temp];} void insert (char * s) {int u = 0, ID; For (; * s; ++ S) {id = idx (* s); If (! Ch [u] [ID]) {memset (CH [size], 0, sizeof (CH [size]); Val [size] = 0; ch [u] [ID] = size ++;} u = CH [u] [ID];} Val [u] + = 1;} int que [maxn]; int front, back; void construct () {front = back = 0; int cur, U; For (INT I = 0; I <sigma_size; ++ I) {u = CH [0] [I]; If (u) {que [back ++] = u; F [u] = 0 ;}} while (front <back) {cur = que [Front ++]; for (INT I = 0; I <sigma_size; ++ I) {u = CH [cur] [I]; If (u) {que [Back ++] = u; F [u] = CH [f [cur] [I]; Val [u] + = Val [f [u];} else ch [cur] [I] = CH [f [cur] [I] ;}}}; ac; int base [4]; int quant [4]; inline int encode () {return quant [0] * base [0] + quant [1] * base [1] + quant [2] * base [2] + quant [3] * base [3 ];} bool vis [maxn] [15010]; int table [maxn] [15010]; int code; // note that each State encoding can be obtained by subtraction recursion, do not recalculate int DFS (int u) {If (vis [u] [Code]) return table [u] [Code]; int & cur = table [u] [Code]; cur = 0; Int ts, temp; For (INT I = 0; I <sigma_size; ++ I) if (quant [I]) {-- quant [I]; code-= base [I]; TS = ac. ch [u] [I]; temp = DFS (TS) + AC. val [ts]; cur = temp> cur? Temp: cur; code + = base [I]; ++ quant [I];} vis [u] [Code] = true; return cur;} void solve () {memset (VIS, 0, sizeof (vis [0]) * AC. size); Code = encode (); DFS (0); printf ("% d \ n", table [0] [Code]);} Char STR [60]; int main () {MP ['a'] = 0; MP ['C'] = 1; MP ['G'] = 2; MP ['T'] = 3; int N, n_case (0); While (scanf ("% d", & N), n) {AC. init (); For (INT I = 0; I <n; ++ I) {scanf ("% s", STR); AC. insert (STR);} AC. construct (); scanf ("% s", STR); quant [0] = quant [1] = quant [2] = quant [3] = 0; for (char * Pi = STR; * PI; ++ PI) ++ quant [MP [* Pi]; base [3] = 1; // If 'A' is used as the low position, it will be TLE. Note that the status of each bit is quant + 1 base [2] = quant [3] + 1; base [1] = (quant [2] + 1) * base [2]; base [0] = (quant [1] + 1) * base [1]; printf ("case % d:", ++ n_case); solve ();} 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.