Exemplo n.º 1
0
void add(int c)
{
    c-='a';
    s[++n]=c;
    int cur=get_fail(last);
    if(!next[cur][c])
    {
        int now=newnode(len[cur]+2);
        fail[now]=next[get_fail(fail[cur])][c];
        next[cur][c]=now;
        num[now]=num[fail[now]]+1;
    }
    last=next[cur][c];
    cnt[last]++;
}
Exemplo n.º 2
0
int KMP(char *T, char *P)   {
    int lent = strlen (T), lenp = strlen (P);
    get_fail (P, lenp);
    int i = 0, j = 0;
    while (i < lent)  {
        while (j != -1 && T[i] != P[j]) j = fail[j];
        i++; j++;
        if (j == lenp)  return (i - j + 1);
    }
    return -1;
}
Exemplo n.º 3
0
int main() {
    int n, m;
    int i, j, k;
    while(scanf("%d%d", &n, &m) != EOF) {
        if(n == 0 && m == 0)break;
        for(i = 1; i <= n; i++) {
            scanf("%lf%lf", &p[i].x, &p[i].y);
        }
        tot = -1;
        clr();
        while(m--) {
            ins();
        }
        get_fail();

        for(i = 0; i <= n; i++) {
            for(j = 0; j <= tot; j++) {
                dp[i][j] = INF;
            }
        }

        int h = node[0].next[0];

        if(node[h].id > 0) {
            puts("Can not be reached!");
            continue;
        }

        dp[1][h] = 0;
        double cost = 0;
        for(i = 1; i <= n; i++) {
            for(j = 0; j <= tot; j++) {
                if(dblcmp(dp[i][j] - INF) == 0)continue;
                for(k = i + 1; k <= n; k++) {
                    h = node[j].next[k - 1];
                    if(node[h].id)continue;
                    cost = disPP(p[i], p[k]) + dp[i][j];
                    if(cost < dp[k][h])dp[k][h] = cost;
                }
            }
        }

        double ans = INF;
        for(i = 0; i <= tot; i++) {
            if(dp[n][i] < ans)ans = dp[n][i];
        }

        if(dblcmp(INF - ans) != 0)printf("%.2f\n", ans);
        else puts("Can not be reached!");

    }
    return 0;
}
Exemplo n.º 4
0
int main()
{
	//freopen("test.txt","r+",stdin);
	while (scanf("%s",p) !=EOF)
	{
		if(p[0] == '.')
			break;
		int r =get_fail();
		//int r =kmp_next();
		printf("%d\n",r);

	}
	//getch();
	return 0;
}
int kmp(char *str, int len1, char *pattern, int len2)
{
    get_fail(pattern, len2);

    int end = len1-1+len2-1, i;
    if(end > 2*len1-1) end = 2*len1-1;

    int k = -1;
    for(i = 0;i <= end;i++)
    {
        char nxt = str[i%len1];

        while(k > -1 && pattern[k+1] != nxt) k = fail[k];

        if(pattern[k+1] == nxt) k++;

        if(k == len2-1)
            return i-len2+1;
    }
    return -1;
}