Hdu_1771 Number Sequence 字符串匹配

题目:匹配

方法:没用KMP,用了字符创hash

/************************************************
Author        :DarkTong
Created Time  :2016/8/14 21:53:07
File Name     :Hdu_1711.cpp
*************************************************/

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ULL;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;

const int maxn = 1100000 + 10;
const ULL x = 100007;
int s[maxn];
ULL p[maxn], h[maxn];
void init()
{
    p[0]=1;
    for(int i=1;i<maxn;++i) p[i]=p[i-1]*x;
}
void Init(int len)
{
    h[len]=0;
    for(int i=len-1;i>=0;--i) h[i]=h[i+1]*p[1]+s[i];
}
ULL Hash(int i, int L)
{
    return h[i]-h[i+L]*p[L];
}
int main()
{
    int T, cas=1, n, m;
    init();
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d%d", &n,&m);
        for(int i=0;i<n;++i) scanf("%d", &s[i]);
        s[n] = INF;
        for(int i=n+1;i<n+m+1;++i) scanf("%d", &s[i]);
        int tn = n+m+1;
        Init(tn);
        int ans = -1;
        for(int i=0;i<n;++i)
        {
            if(Hash(i, m)==Hash(n+1, m))
            {
                ans = i+1; break;
            }
        }
        printf("%d\n", ans);
    }

    return 0;
}

时间: 2024-12-20 16:08:35

Hdu_1771 Number Sequence 字符串匹配的相关文章

HDU 1711 Number Sequence(字符串匹配)

Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10571    Accepted Submission(s): 4814 Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1],

LeetCode 44 Wildcard Matching(字符串匹配问题)

题目链接:https://leetcode.com/problems/wildcard-matching/?tab=Description '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). 字符串匹配问题. 如上所示:其中 ‘ ?’ 可以匹配任何一个单字符    ’ * ‘ 可以匹配任意长度的字符包括空字符串 给定字符串s,和字符串p.判

HDU - 1711 Number Sequence KMP字符串匹配

HDU - 1711 Number Sequence Time Limit: 5000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M <= 10000, 1 <=

SDJZU_新生_字符串匹配(KMP)_A - Number Sequence

A - Number Sequence Crawling in process... Crawling failed Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M &l

POJ3080——Blue Jeans(暴力+字符串匹配)

Blue Jeans DescriptionThe Genographic Project is a research partnership between IBM and The National Geographic Society that is analyzing DNA from hundreds of thousands of contributors to map how the Earth was populated. As an IBM researcher, you hav

poj3080--Blue Jeans(字符串匹配)

Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12233   Accepted: 5307 Description The Genographic Project is a research partnership between IBM and The National Geographic Society that is analyzing DNA from hundreds of thousa

Rabin_Karp(hash) HDOJ 1711 Number Sequence

题目传送门 1 /* 2 Rabin_Karp:虽说用KMP更好,但是RK算法好理解.简单说一下RK算法的原理:首先把模式串的哈希值算出来, 3 在文本串里不断更新模式串的长度的哈希值,若相等,则找到了,否则整个模式串的长度的哈希值向右移动一位 4 */ 5 /************************************************ 6 * Author :Running_Time 7 * Created Time :2015-8-5 14:04:26 8 * File

HDU 1711 Number Sequence(KMP算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 15548    Accepted Submission(s): 6836 Problem Description Given two sequence

hdu 1711 Number Sequence KMP 基础题

Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11691    Accepted Submission(s): 5336 Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1],