hdu1711 hdu1686 hdu3336 hdu1358

hdu 1711

kmp算法模板题,没明白问什么这里下标一定要是1开始,0开始就错了,或许是我还有地方没想到只是过了样例而已,改为1过了


代码:

#include <iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<algorithm>
using namespace std;
const int LEN1=1000105;
const int LEN2=10105;
int s[LEN1], t[LEN2], next[LEN2]; 
void get_next(const int&m)
{
    int i=1, j=0; next[1]=0;
    while( i<=m )
    {
        if( 0==j || t[i]==t[j] )
        {
            ++i; ++j; next[i]=j;
        }
        else j=next[j];
    }
}
int func(const int&n, const int&m)
{
    int i=1, j=1;
    while( i<=n && j<=m )
    {
        if( 0==j || s[i]==t[j] )
        {
            ++i; ++j;
        }
        else j=next[j];
    }
    if( j>m ) return i-m;
    return -1;
}
int main(int argc, char *argv[])
{
    int T, n, m;
    cin>>T;
    while( T-- )
    {
        scanf("%d%d", &n, &m);
        for(int i=1; i<=n; i++) scanf("%d", s+i);
        for(int i=1; i<=m; i++) scanf("%d", t+i);
        get_next(m);
        printf("%d\n", func(n, m));
    }
    return 0;
}



hdu 1686

和hdu 1711差不多,不过这里是求s串里最大有几个t串

代码:

#include <iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<algorithm>
using namespace std;
const int LEN1=1000105;
const int LEN2=10105;
char s[LEN1], t[LEN2];
int next[LEN2]; 
void get_next()
{
	int i=0, j=-1, m=strlen(t); next[0]=-1;
	while( i<m )
	{
		if( -1==j || t[i]==t[j] )
		{
			++i; ++j; next[i]=j;
		}
		else j=next[j];
	}
}
int kmp()
{
	int i=0, j=0, n=strlen(s), m=strlen(t);
	int times=0;
	while( i<n )
	{
		if( -1==j || s[i]==t[j] )
		{
			++i; ++j;
		}
		else j=next[j];
		/*这里不是j=0;因为i到这里 s串的 i-next[j]+1 ~~~ i-1会和
		 t串的 0 ~~~ next[j]-1相同但不一定s[i]和t[0]相同,所以如果
		 j=0会减少最大子串的数目; 
		*/ 
		if( j>=m ) times++, j=next[j];
	}
	return times;
}
int main(int argc, char *argv[])
{
	int T;
	cin>>T;
	while( T-- )
	{
		scanf("%s", t);
		scanf("%s", s);
		get_next();
		printf("%d\n", kmp());
	}
	return 0;
}


hdu1358:

思路:因为此题是一个子字符串循环得到的母串,所以没有特判;直接就是算后面的是前面的小字符串的多少倍,利用next数组,因为

next数组存放的是与前缀相同的最长的序列下标到了哪里;所以  i - next[i]  表示next[i]+1 ~ i 都是1 ~ next[i] 的字符串复制得到;所以做就行了

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<algorithm>
using namespace std;
const int LEN=1000005;
const int INF=0x3f3f3f3f;
const int MOD=10007;
int next[LEN];  
char s[LEN], t[LEN];
int cns[LEN];
int main(int argc, char *argv[])
{
	int i, j, lt, cas=0;
	while( scanf("%d", <)!=EOF && lt )
	{
		getchar();
		scanf("%s", t+1);
		i=1; j=0; next[1]=0;
		while( i<=lt )
		{
			if( 0==j || t[i]==t[j] )
			{
				++i; ++j;
				next[i]=j;
			}
			else j=next[j];
		}
		for(i=1; i<=lt; i++) cout<<next[i]<<" "; cout<<endl;
		printf("Test case #%d\n", ++cas);
		for(i=3, lt++; i<=lt; i++)
		{
			j=(i-1)/(i-next[i]);
			if( j>1 && (i-1)%(i-next[i])==0 )
				printf("%d %d\n", i-1, j);
		}
		printf("\n");
	}
	return 0;
}


hdu3336

思路:

代码:

#include <iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<algorithm>
using namespace std;
const int LEN = 1000005;
const int MOD=10007;
char t[LEN];
int next[LEN];
int main(int argc, char *argv[])
{
    int n, i, j, T;
    cin>>T;
    while( T-- )
    {
        scanf("%d%s", &n, t);
        i=0; j=-1; next[0]=-1;
        while( i<n )
        {
            if( -1==j || t[i]==t[j] ) next[++i]=++j;
            else j=next[j];
        }
        //for(i=0; i<n; i++) cout<<next[i]<<" ";
        //cout<<endl;
        int sum=0, pos;
        for(i=1; i<=n; i++)
        {
            pos=i;
            while( pos )
            {
                sum=(sum+1)%MOD;
                pos=next[pos];
            }
        }
        printf("%d\n", sum);
    }
    return 0;
}










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值