UVa 1449 Dominating Patterns

方法:AC自动机

题意比较明显,用ac 自动机。陷阱是可能会出现重复的string。

code:

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <algorithm>
  4 #include <iostream>
  5 #include <string>
  6 #include <vector>
  7 #include <stack>
  8 #include <bitset>
  9 #include <cstdlib>
 10 #include <cmath>
 11 #include <set>
 12 #include <list>
 13 #include <deque>
 14 #include <map>
 15 #include <queue>
 16 #include <fstream>
 17 #include <cassert>
 18 #include <unordered_map>
 19 #include <unordered_set>
 20 #include <cmath>
 21 #include <sstream>
 22 #include <time.h>
 23 #include <complex>
 24 #include <iomanip>
 25 #define Max(a,b) ((a)>(b)?(a):(b))
 26 #define Min(a,b) ((a)<(b)?(a):(b))
 27 #define FOR(a,b,c) for (ll (a)=(b);(a)<(c);++(a))
 28 #define FORN(a,b,c) for (ll (a)=(b);(a)<=(c);++(a))
 29 #define DFOR(a,b,c) for (ll (a)=(b);(a)>=(c);--(a))
 30 #define FORSQ(a,b,c) for (ll (a)=(b);(a)*(a)<=(c);++(a))
 31 #define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
 32 #define FOREACH(a,b) for (auto &(a) : (b))
 33 #define rep(i,n) FOR(i,0,n)
 34 #define repn(i,n) FORN(i,1,n)
 35 #define drep(i,n) DFOR(i,n-1,0)
 36 #define drepn(i,n) DFOR(i,n,1)
 37 #define MAX(a,b) a = Max(a,b)
 38 #define MIN(a,b) a = Min(a,b)
 39 #define SQR(x) ((LL)(x) * (x))
 40 #define Reset(a,b) memset(a,b,sizeof(a))
 41 #define fi first
 42 #define se second
 43 #define mp make_pair
 44 #define pb push_back
 45 #define all(v) v.begin(),v.end()
 46 #define ALLA(arr,sz) arr,arr+sz
 47 #define SIZE(v) (int)v.size()
 48 #define SORT(v) sort(all(v))
 49 #define REVERSE(v) reverse(ALL(v))
 50 #define SORTA(arr,sz) sort(ALLA(arr,sz))
 51 #define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
 52 #define PERMUTE next_permutation
 53 #define TC(t) while(t--)
 54 #define forever for(;;)
 55 #define PINF 1000000000000
 56 #define newline ‘\n‘
 57
 58 #define test if(1)if(0)cerr
 59 using namespace std;
 60 using namespace std;
 61 typedef vector<int> vi;
 62 typedef vector<vi> vvi;
 63 typedef pair<int,int> ii;
 64 typedef pair<double,double> dd;
 65 typedef pair<char,char> cc;
 66 typedef vector<ii> vii;
 67 typedef long long ll;
 68 typedef unsigned long long ull;
 69 typedef pair<ll, ll> l4;
 70 const double pi = acos(-1.0);
 71
 72
 73 const int MAXNODE = 11000+5;
 74 const int SIGMA_SIZE = 26;
 75
 76
 77 int n;
 78 string text, input[151];
 79 map<string, int> ms;
 80
 81
 82 inline int idx(char c) { return c-‘a‘; }
 83 int cnt[151];
 84 struct AC
 85 {
 86     int g[MAXNODE][SIGMA_SIZE], f[MAXNODE], val[MAXNODE], last[MAXNODE];
 87     int sz;
 88     int newnode()
 89     {
 90         memset(g[sz], 0, sizeof(g[sz]));
 91         val[sz] = 0;
 92         return sz++;
 93     }
 94     void init()
 95     {
 96         sz = 0; newnode();
 97     }
 98     void insert(const string &str, int v)
 99     {
100         int u = 0, n = str.length();
101         for (int i = 0; i < n; ++i)
102         {
103             int c = idx(str[i]);
104             if (!g[u][c]) g[u][c] = newnode();
105             u = g[u][c];
106         }
107         val[u] = v;
108     }
109     void print(int j)
110     {
111         if (j)
112         {
113             ++cnt[val[j]];
114
115             // operation
116             print(last[j]);
117         }
118     }
119     void find(const string &str)
120     {
121         int n = str.length(), j = 0;
122         for (int i = 0; i < n; ++i)
123         {
124             int c = idx(str[i]);
125             j = g[j][c];
126
127             if (val[j]) print(j);
128             else if (last[j]) print(last[j]);
129         }
130     }
131     void get_fail()
132     {
133         queue<int> q; f[0] = 0; last[0] = 0;
134         for (int c = 0; c < SIGMA_SIZE; ++c)
135         {
136             int u = g[0][c];
137             if (u) { f[u] = 0; q.push(u); last[u] = 0; }
138         }
139         while (!q.empty())
140         {
141             int r = q.front();  q.pop();
142             for (int c = 0; c < SIGMA_SIZE; ++c)
143             {
144                 int u = g[r][c];
145                 if (!u) { g[r][c] = g[f[r]][c]; continue; }
146                 q.push(u);
147                 int v = f[r];
148                 while (v && !g[v][c]) v = f[v];
149                 f[u] = g[v][c];
150                 last[u] = val[f[u]]?f[u]:last[f[u]];
151             }
152         }
153     }
154
155 };
156
157 AC ac;
158
159 int main()
160 {
161     ios::sync_with_stdio(false);
162     cin.tie(0);
163     while (cin >> n && n)
164     {
165         ms.clear();
166         Reset(cnt, 0);
167         ac.init();
168         repn(i, n)
169         {
170             cin >> input[i];
171             ms[input[i]] = i;
172         }
173         cin >> text;
174         for (auto &pr : ms)
175         {
176             ac.insert(pr.first, pr.second);
177         }
178         ac.get_fail();
179         ac.find(text);
180         int best = 0;
181         for (int i = 1; i <= n; ++i) best = max(best, cnt[i]);
182         cout << best << newline;
183         for (int i = 1; i <= n; ++i)
184             if (cnt[ms[input[i]]] == best)
185                 cout << input[i] << newline;
186     }
187 }

时间: 2024-11-05 12:09:39

UVa 1449 Dominating Patterns的相关文章

uva 1449 - Dominating Patterns(AC自动机)

题目练级:uva 1449 - Dominating Patterns 题目大意:有一个由小写字母组成的字符串集和一个文本T,要求找出那些字符串在文本中出现的次数最多. 解题思路:将字符串集建立AC自动机,然后传入T进行匹配,对每个匹配上的字符串多应次数加1,最后找出最大值.出现次数与最大值相同的字符串输出.注意字符集中出现相同字符的情况. #include <cstdio> #include <cstring> #include <queue> #include &l

【UVA】1449-Dominating Patterns(AC自动机)

AC自动机的模板题,需要注意的是,对于每个字符串,需要利用map将它映射到一个结点上,这样才能按顺序输出结果. 14360841 1449 Dominating Patterns Accepted C++ 0.146 2014-10-16 11:41:35 #include<stack> #include<queue> #include<map> #include<set> #include<cstdio> #include<cstring

【UVA】1449-Dominating Patterns(AC自己主动机)

AC自己主动机的模板题.须要注意的是,对于每一个字符串,须要利用map将它映射到一个结点上,这样才干按顺序输出结果. 14360841 1449 option=com_onlinejudge&Itemid=8&page=show_problem&problem=4195" style="font-size:13.3333330154419px; margin:0px; padding:0px; color:rgb(153,0,0); text-decoratio

Dominating Patterns

Dominating Patterns Time Limit:3000MS   Memory Limit:Unknown   64bit IO Format:%lld & %llu Description The archaeologists are going to decipher a very mysterious ``language". Now, they know many language patterns; each pattern can be treated as a

[UVA1149]Dominating Patterns

Description The archaeologists are going to decipher a very mysterious ``language". Now, they know many language patterns; each pattern can be treated as a string on English letters (only lower case). As a sub string, these patterns may appear more t

uva 269 - Counting Patterns(构造)

题目链接:uva 269 - Counting Patterns 题目大意:给出n和k,要求找出满足的序列,要求为n元组,由-k到k组成,并且和为0.求出所有满足的元组个数,并且对于左移,右移,水平翻转,每个元素取相反数相同的视为一种,用字典序最大的表示,输出按照字典序最小的输出. 解题思路:因为表示的时候按照字典序最大的表示,一开始枚举开头的位置,那么在后面的数的绝对值就不会大于该数.最后判断一下,如果该序列不是最优的表示方法,就不是该情况. #include <cstdio> #inclu

LA4670 Dominating Patterns AC自动机模板

Dominating Patterns 每次看着别人的代码改成自己的模板都很头大...空间少了个0卡了好久 裸题,用比map + string更高效的vector代替蓝书中的处理方法 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <algorithm> 6 #include <queue> 7

UVALive 4670 Dominating Patterns --AC自动机第一题

题意:多个模板串,一个文本串,求出那些模板串在文本串中出现次数最多. 解法:AC自动机入门模板题. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <algorithm> #include <string> #include <vector> #in

uvalive 4670 Dominating Patterns

在文本串中找出现次数最多的子串. 思路:AC自动机模板+修改一下print函数. 1 #include<stdio.h> 2 #include<math.h> 3 #include<stdio.h> 4 #include<stdlib.h> 5 #include<iostream> 6 #include<string> 7 #include<memory.h> 8 #include<map> 9 #includ