HDOJ 1711 Number Sequence

本文介绍了一种高效的字符串匹配算法——KMP算法,并提供了详细的实现步骤和AC代码示例。通过预处理模式串来提高匹配效率,适用于需要频繁进行字符串搜索的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:给出一个模式串和一个主串,找出模式串在主串中第一次匹配的位置,不存在匹配时输出-1

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711

思路:KMP,当有匹配时输出起点位置即可

注意点:无


以下为AC代码:

Run IDSubmit TimeJudge StatusPro.IDExe.TimeExe.MemoryCode Len.LanguageAuthor
130364422015-03-05 19:40:56Accepted17111248MS12912K2616 BG++luminous11

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <iomanip>
#include <cstdlib>
#include <algorithm>
//#include <unordered_map>
//#include <unordered_set>
#define ll long long
#define ull unsigned long long
#define all(x) (x).begin(), (x).end()
#define clr(a, v) memset( a , v , sizeof(a) )
#define pb push_back
#define RDI(a) scanf ( "%d", &a )
#define RDII(a, b) scanf ( "%d%d", &a, &b )
#define RDIII(a, b, c) scanf ( "%d%d%d", &a, &b, &c )
#define RDS(s) scanf ( "%s", s )
#define PIL(a) printf ( "%d\n", a )
#define PIIL(a,b) printf ( "%d %d\n", a, b )
#define PIIL(a,b,c) printf ( "%d %d %d\n", a, b, c )
#define PSL(s) printf ( "%s\n", s )
#define REP(i,m,n) for ( int i = m; i <= n; i ++ )
#define DEP(i,m,n) for ( int i = m; i >= n; i -- )
#define REPI(i,m,n,k) for ( int i = m; i <= n; i += k )
#define DEPI(i,m,n,k) for ( int i = m; i >= n; i -= k )
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
using namespace std;
const double pi = acos(-1);
const double eps = 1e-10;
const int dir[4][2] = { 1,0, -1,0, 0,1, 0,-1 };
struct node{
    int x, y, cnt;
    node(){}
    node( int _x, int _y ) : x(_x), y(_y) {}
    node( int _x, int _y, int _cnt ) : x(_x), y(_y), cnt(_cnt) {}
};
int pre[1000010];
int x[1000010];
int y[1000010];
void preKMP ( int x[], int m )
{
    int i, j;
    j = pre[0] = -1;
    i = 0;
    while ( i < m ){
        while ( j != -1 && x[i] != x[j] ) j = pre[j];
        pre[++i] = ++j;
        //if ( x[++i] == x[++j] ) pre[i] = pre[j];
        //else pre[i] = j;
    }
}

int kmp ( int x[], int m, int y[], int n )
{
    int i, j;
    int ans = 0;
    preKMP ( x, m );
    i = j = 0;
    while ( i < n ){
        while ( j != -1 && x[j] != y[i] ) j = pre[j];
        //cout << x[j] << ' ' << y[i] << ' ' << i << ' ' << j << endl;
        i ++;
        j ++;
        if ( j >= m ){
            return i - j + 1;
        }
    }
    return -1;
}
int main()
{
    int T;
    RDI ( T );
    while ( T -- ){
        int m, n;
        clr ( pre, 0 );
        clr ( x, 0 );
        clr ( y, 0 );
        RDII ( n, m );
        REP ( i, 0, n - 1 ) RDI ( x[i] );
        REP ( i, 0, m - 1 ) RDI ( y[i] );
        PIL ( kmp ( y, m, x, n ) );
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值