zoj 3228 Searching the String【ac自动机】
来源:互联网 发布:国外源码网站 编辑:程序博客网 时间:2024/05/05 16:40
28964542012-05-18 16:35:19Accepted3228C++210072084zz_121528964412012-05-18 16:30:35Compilation Error3228C++00zz_121528964252012-05-18 16:26:32Compilation Error3228C++00zz_121528963952012-05-18 16:11:15Wrong Answer3228C++198078916zz_121528963902012-05-18 16:05:16Time Limit Exceeded3228C++700175364zz_121528963812012-05-18 15:57:51Time Limit Exceeded3228C++70010zz_1215
一开始用了个vector保存id,以为能ac,没想到数据这么变态,肯定有好多重复的,所以超时了,注释掉find()函数之后再交,1980ms,说明其他的操作不会超时,加一个link数组解决这个问题,在建立字典树的时候顺便把相同的连接起来,没想到居然会CE,我了个擦的我可不是拿记事本写的啊!!在codeblock上和devc上都编译通过了啊,再交一次,一看返回,居然有个函数的名字也是link。。。我就晕了,把link改成linking。。ac
#include<iostream>#include<vector>#include<algorithm>#include<cstdio>#include<queue>#include<stack>#include<string>#include<map>#include<set>#include<cmath>#include<cassert>#include<cstring>#include<iomanip>using namespace std;#ifdef _WIN32#define i64 __int64#define out64 "%I64d\n"#define in64 "%I64d"#else#define i64 long long#define out64 "%lld\n"#define in64 "%lld"#endif#define FOR(i,a,b) for( int i = (a) ; i <= (b) ; i ++)#define FF(i,a) for( int i = 0 ; i < (a) ; i ++)#define FFD(i,a) for( int i = (a)-1 ; i >= 0 ; i --)#define S64(a) scanf(in64,&a)#define SS(a) scanf("%d",&a)#define LL(a) ((a)<<1)#define RR(a) (((a)<<1)+1)#define pb push_back#define CL(Q) while(!Q.empty())Q.pop()#define MM(name,what) memset(name,what,sizeof(name))#define read freopen("in.txt","r",stdin)#define write freopen("out.txt","w",stdout)const int inf = 0x3f3f3f3f;const i64 inf64 = 0x3f3f3f3f3f3f3f3fLL;const double oo = 10e9;const double eps = 1e-10;const double pi = acos(-1.0);const int maxz = 600011;const int maxn = 100011;const int head = 0;struct zz{ int to[26]; int fail; int id;}zx[maxz];char sc[maxn];string rs,s[maxn];int flag[maxn];int n,use;int at[maxn];int have[maxn][3];int linking[maxn];bool ok[maxn];inline int get(){ use++; zx[use].fail = 0; zx[use].id = 0; MM(zx[use].to,-1); return use;}void insert(int id,string & p){ int now = head; int c; for(int i=0;i<p.length();i++) { c = p[i]-'a'; if(zx[now].to[c]!=-1) { now = zx[now].to[c]; } else { zx[now].to[c] = get(); now = zx[now].to[c]; } } if(zx[now].id) { ok[id] = true; linking[id] = zx[now].id; } else { zx[now].id = id; } return ;}void ac(){ queue<int>q; CL(q); q.push(head); int now,to,temp; while(!q.empty()) { now = q.front(); q.pop(); for(int i=0;i<26;i++) { if(zx[now].to[i]!=-1) { to = zx[now].to[i]; q.push(to); temp = now; while(temp) { temp = zx[temp].fail; if(zx[temp].to[i]!=-1) { temp = zx[temp].to[i]; break; } } zx[to].fail = temp; } } } return ;}void find(int pos,int now){ if(now) { int len,id = zx[now].id; if(id) { have[id][0]++; len = s[id].length(); if( pos- len + 1 > at[id] + len - 1) { have[id][1]++; at[id] = pos - s[id].length() + 1; } } find(pos,zx[now].fail); } return ;}void ak(string & p){ int now = head; int temp; int c; for(int i=0;i<p.length();i++) { c = p[i] - 'a'; if(zx[now].to[c] != -1) { now = zx[now].to[c]; } else { temp = now; while(now) { now = zx[now].fail; if(zx[now].to[c]!= -1) { now = zx[now].to[c]; break; } } zx[temp].to[c] = now; } find(i,now); } return ;}void start(){ ac(); for(int i=0;i<=n;i++) { at[i] = -inf; have[i][0] = have[i][1] = 0; } ak(rs); return ;}int main(){ int tt = 1; char sd[11]; while(scanf("%s",sc)!=EOF) { MM(ok,false); rs = sc; SS(n); use = -1; get(); for(int i=1;i<=n;i++) { SS(flag[i]); scanf("%s",sd); s[i] = sd; insert(i,s[i]); } start(); printf("Case %d\n",tt++); for(int i=1;i<=n;i++) { if(!ok[i]) { printf("%d\n",have[i][flag[i]]); } else { printf("%d\n",have[linking[i]][flag[i]]); } } printf("\n"); } return 0;}
- zoj 3228 Searching the String【ac自动机】
- ZOJ 3228 Searching the String(AC自动机)
- ZOJ 3228 Searching the String(AC自动机)
- zoj -- 3228 Searching the String(AC自动机)
- ZOJ - 3228 Searching the String (AC自动机)
- zoj 3228 Searching the String(AC自动机)
- ZOJ 3228 Searching the String AC自动机
- zoj 3228 Searching the String (ac自动机)
- zoj 3228 Searching the String 【AC自动机】
- ZOJ 3228 Searching the String(AC自动机)
- [AC自动机] zoj Searching the String
- ZOJ Problem Set - 3228 Searching the String AC自动机
- ZOJ 3228 Searching the String (AC自动机)
- ZOJ 3228 Searching the String(AC自动机)
- ZOJ 3228 Searching the String (AC自动机)
- zoj 3228 Searching the String(AC自动机基本应用)
- ZOJ 3328 Searching the String (AC自动机)
- ZOJ 3228Searching the String AC自动机的不重复匹配
- 得到电话状态(WIFI状态,厂商,运营商等)的工具类
- JSP使用存储过程
- 神秘的auto_ptr
- TCP 、UDP、IP包的最大长度是多少?
- Python os模块
- zoj 3228 Searching the String【ac自动机】
- 犯了个极其郁闷的小错误
- ajax的初次使用
- 中国核桃油10大品牌-汇智源核桃油首榜
- OpenGL 各个shader的作用和区别
- SQL语句like子句中的转义符
- MSSQL 实现 group_concat
- 判断浏览器 Firefox Opera Safari IE5 6 7 8 各版本
- UNIX的几个主要进程