sdut_2463_学密码学一定得学程序

学密码学一定得学程序

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic Discuss

Problem Description

曾经,ZYJ同学非常喜欢密码学。有一天,他发现了一个很长很长的字符串S1。他很好奇那代表着什么,于是神奇的WL给了他另一个字符串S2。但是很不幸的是,WL忘记跟他说是什么意思了。这个时候,ZYJ不得不求助与伟大的ZP。ZP笑了笑说,这个很神奇的,WL的意思是只要你找到她给你的字符串在那个神奇的字符串的位置,你就会有神奇的发现。ZYJ恍然大悟,原来如此,但是悲剧来了,他竟然不知道怎么找。。。。是的,很囧是不是。所以这时候就需要化身为超级玛丽亚的你现身了,告诉他吧。。。。。。

Input

首先输入一个n。表示有n组测试数据。

每组测试数据有两行。

第一行为字符串S1,长度不大于1000000。

第二行为字符串S2,长度不大于10000,并且长度不小于2。

Output

输出S2在S1的位置。如果有多个位置,只输出第一个位置。

如果找不到,就输出“::>_<::“(不输出双引号)。

Sample Input

1
ASDFGDF
DF

Sample Output

3

Hint

Source

ZP

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char s1[1000010];
char s2[10100];
int next[10010];

void get_next(int l)
{
    int i = 0;
    int j = -1;
    next[0] = -1;
    while(i < l)
    {
        if(j == -1 || s2[i] == s2[j])
        {
            i++;
            j++;
            next[i] = j;
        }
        else
            j = next[j];
    }
}

void kmp(int l, int s)
{
    int i = 0, j = 0;
    while(i < s && j < l)
    {
        if(j == -1 || s1[i] == s2[j])
        {
            i++;
            j++;
        }
        else
            j = next[j];
    }
    if(j == l)
        printf("%d\n", i - j + 1);
    else
        printf("::>_<::\n");
}

int main()
{
    int l, s;
    int n;
    scanf("%d", &n);
    while(n--)
    {
        scanf("%s", s1);
        scanf("%s", s2);
        s = strlen(s1);
        l = strlen(s2);
        get_next(l);
        kmp(l, s);
    }
    return 0;
}
 

猜你喜欢

转载自blog.csdn.net/strongerXiao/article/details/81458049