zoj 3228 Searching the String【ac自动机】

2896454 2012-05-18 16:35:19 Accepted 3228 C++ 2100 72084 zz_1215
2896441 2012-05-18 16:30:35 Compilation Error 3228 C++ 0 0 zz_1215
2896425 2012-05-18 16:26:32 Compilation Error 3228 C++ 0 0 zz_1215
2896395 2012-05-18 16:11:15 Wrong Answer 3228 C++ 1980 78916 zz_1215
2896390 2012-05-18 16:05:16 Time Limit Exceeded 3228 C++ 7001 75364 zz_1215
2896381 2012-05-18 15:57:51 Time Limit Exceeded 3228 C++ 7001 0 zz_1215
一开始用了个vector保存id,以为能ac,没想到数据这么变态,肯定有好多重复的,所以超时了,注释掉find()函数之后再交,1980ms,说明其他的操作不会超时,加一个link数组解决这个问题,在建立字典树的时候顺便把相同的连接起来,没想到居然会CE,我了个擦的我可不是拿记事本写的啊!!在codeblock上和devc上都编译通过了啊,再交一次,一看返回,居然有个函数的名字也是link。。。我就晕了,把link改成linking。。ac
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
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;iq;
    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


你可能感兴趣的:(zoj 3228 Searching the String【ac自动机】)