Learning cryptography must learn the procedure.

Source: Internet
Author: User

Title Description

Once, Zyj classmates like cryptography very much. One day, he found a very long long string of S1. He was curious as to what it represented, and the magical WL gave him another string S2. But unfortunately, WL forgot to tell him what it meant. At this time, Zyj had to ask for help with the great ZP. Zp smiled and said, this is magical, wl means that as long as you find the string she gave you in the location of the magical string, you will have a magical discovery. Zyj suddenly, so, but the tragedy came, he unexpectedly did not know how to find .... Yes, it's embarrassing, isn't it? So this time you need to become Super Maria's you show up, tell him ...

Input

First, enter an N. Indicates that there are n sets of test data.

There are two rows for each set of test data.

The first behavior string S1, the length is not greater than 1000000.

The second behavior of the string S2, not greater than 10000, and the length is not less than 2.

Output

The output S2 is in the S1 position. If there are multiple locations, only the first position is output.

If it is not found, the output is ":: >_<::" (no double quotation marks are output).

Sample input
1ASDFGDFDF
Sample output
3

Because string lengths are not very large, methods that can be matched directly with strings do not time out, so there are two ways

Method One:

#include <stdio.h> #include <string.h> #define N 1000001char s1[n],s2[10010];int main () {int n;scanf ("%d"), &n); GetChar (); while (n--) {gets (S1);  Gets (S2); char *s3=strstr (S1,S2);  if (s3==null)  {    printf (":: >_<::\n");  }  else  {int    k=s3-s1+1;    printf ("%d\n", K);  

method Two: KMP algorithm

#include <stdio.h> #include <string.h> #define N 1000001char s1[n],s2[10001];int next[n];void getnext () {  int i=0,j=-1;  Next[0]=-1;  while (s2[i]!= ')  {    if (J==-1 | | s2[i]==s2[j])    {      i++;      j + +;      next[i]=j;    }    else     j=next[j];}  } void KMP () {  int i=0,j=0;  GetNext ();  int Len1=strlen (s1);  int Len2=strlen (s2);  while (I<len1 && j<len2)  {    if (J==-1 | | s1[i]==s2[j])    {     i++;     j + +;    }    else    j=next[j];  }  if (j>=len2)  {    printf ("%d\n", i-len2+1);  }  else  {    printf (":: >_<::\n");}  } int main () {  int n;  scanf ("%d", &n);  GetChar ();  while (n--)  {    gets (S1);    Gets (S2);    KMP ();  }  return 0;}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Learning cryptography must learn the procedure.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.