HDU 2896 AC automatic machine

Source: Internet
Author: User
Tags acos

The meaning of the question is not explained.

I am stuck in this question. I thought it was 26 letters at first (I did not read the question carefully and read the result of the example below ), then I ressed several rounds and found that the description in the question was a visible character in the ASCLL code table. Then I changed the 0-25 cycle during the dictionary tree creation process to 0-127.

Let's talk about the idea. This is the template Question of the AC automatic machine. The only thing to note is to add an id field to store the serial number of this string, the final output should be in the ascending order. I directly threw all the output into the set output.

But I think the data is a bit watery... I always feel that A is not very steadfast.

The memory is added and released, but the memory is not less than the original one. Is there only one set of o

 

# Include <iostream> # include <cstdio> # include <algorithm> # include <string> # include <cmath> # include <cstring> # include <queue> # include <set> # include <vector> # include <stack> # include <map> # include <iomanip> # define PI acos (-1.0) # define Max 2505 # define inf 1 <28 # define LL (x) (x <1) # define RR (x) (x <1 | 1) # define REP (I, s, t) for (int I = (s); I <= (t); ++ I) # define ll long Long # define mem (a, B) memset (a, B, sizeof (a) # define mp (a, B) make_pair (a, B) # define PII pair <int, int> using namespace std; struct node {node * fail; node * next [128]; int count; int id; node () {fail = 0; count = 0; mem (next, 0); id = 0 ;}} * qe [1000005]; node * root = 0; // insert a []. void insert (char * a, int id) {node * p = root; int l = strlen (a); for (int I = 0; I <l; I ++) {int Tt = a [I]; if (p-> next [tt] = 0) {p-> next [tt] = new node ();} p = p-> next [tt];} p-> count ++; p-> id = id;} // build * fail. void build () {root-> fail = 0; int h = 0, t = 0; qe [h ++] = root; while (h> t) {node * temp = qe [t ++]; node * p = 0; for (int I = 0; I <128; I ++) {if (temp-> next [I]! = 0) {if (temp = root) temp-> next [I]-> fail = root; else {p = temp-> fail; while (p! = 0) {if (p-> next [I]! = 0) {temp-> next [I]-> fail = p-> next [I]; // find the matching break;} p = p-> fail ;} if (p = 0) temp-> next [I]-> fail = root; // if not found, point fail to root} qe [h ++] = temp-> next [I] ;}}} set <int> anss [1111]; int search (char * a, int id) {int l = strlen (a); node * p = root; int ans = 0; for (int I = 0; I <l; I ++) {int tt = a [I]; while (p-> next [tt] = 0 & p! = Root) p = p-> fail; p = p-> next [tt]; p = (p = 0 )? Root: p; node * temp = p; while (temp! = Root & temp-> count! = 0) {ans + = temp-> count; anss [id]. insert (temp-> id); // temp-> count =-1; temp = temp-> fail;} return ans;} void deleteAll (node * p) {for (int I = 0; I <128; I ++) if (p-> next [I]! = 0) {deleteAll (p-> next [I]);} delete p;} char a [11111]; int num [1111]; int main () {int n; cin> n; getchar (); root = new node (); for (int I = 1; I <= n; I ++) {gets (); insert (a, I);} int m; cin> m; getchar (); build (); for (int I = 1; I <= m; I ++) {gets (a); num [I] = search (a, I);} int cc = 0; for (int I = 1; I <= m; I ++) {if (num [I]) {printf ("web % d:", I); for (se T <int>: iterator p = anss [I]. begin (); p! = Anss [I]. end (); ++ p) cout <"<* p; cc ++; puts (" ") ;}} printf (" total: % d \ n ", cc); deleteAll (root); return 0 ;} # include <iostream> # include <cstdio> # include <algorithm> # include <string> # include <cmath> # include <cstring> # include <queue> # include <set> # include <vector> # include <stack> # include <map> # include <iomanip> # define PI acos (-1.0) # define Max 2505 # define inf 1 <28 # define LL (x) (x <1) # Define RR (x) (x <1 | 1) # define REP (I, s, t) for (int I = (s ); I <= (t); ++ I) # define ll long # define mem (a, B) memset (a, B, sizeof ()) # define mp (a, B) make_pair (a, B) # define PII pair <int, int> using namespace std; struct node {node * fail; node * next [128]; int count; int id; node () {fail = 0; count = 0; mem (next, 0); id = 0 ;}} * qe [1000005]; node * root = 0; // insert a []. void inse Rt (char * a, int id) {node * p = root; int l = strlen (a); for (int I = 0; I <l; I ++) {int tt = a [I]; if (p-> next [tt] = 0) {p-> next [tt] = new node ();} p = p-> next [tt];} p-> count ++; p-> id = id;} // build * fail. void build () {root-> fail = 0; int h = 0, t = 0; qe [h ++] = root; while (h> t) {node * temp = qe [t ++]; node * p = 0; for (int I = 0; I <128; I ++) {if (te Mp-> next [I]! = 0) {if (temp = root) temp-> next [I]-> fail = root; else {p = temp-> fail; while (p! = 0) {if (p-> next [I]! = 0) {temp-> next [I]-> fail = p-> next [I]; // find the matching break;} p = p-> fail ;} if (p = 0) temp-> next [I]-> fail = root; // if not found, point fail to root} qe [h ++] = temp-> next [I] ;}}} set <int> anss [1111]; int search (char * a, int id) {int l = strlen (a); node * p = root; int ans = 0; for (int I = 0; I <l; I ++) {int tt = a [I]; while (p-> next [tt] = 0 & p! = Root) p = p-> fail; p = p-> next [tt]; p = (p = 0 )? Root: p; node * temp = p; while (temp! = Root & temp-> count! = 0) {ans + = temp-> count; anss [id]. insert (temp-> id); // temp-> count =-1; temp = temp-> fail;} return ans;} void deleteAll (node * p) {for (int I = 0; I <128; I ++) if (p-> next [I]! = 0) {deleteAll (p-> next [I]);} delete p;} char a [11111]; int num [1111]; int main () {int n; cin> n; getchar (); root = new node (); for (int I = 1; I <= n; I ++) {gets (); insert (a, I);} int m; cin> m; getchar (); build (); for (int I = 1; I <= m; I ++) {gets (a); num [I] = search (a, I);} int cc = 0; for (int I = 1; I <= m; I ++) {if (num [I]) {printf ("web % d:", I); for (set <in T>: iterator p = anss [I]. begin (); p! = Anss [I]. end (); ++ p) cout <"<* p; cc ++; puts (" ") ;}} printf (" total: % d \ n ", cc); deleteAll (root); 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.