1570. [POJ3461] Wu Bo, 1570poj3461

Source: Internet
Author: User

1570. [POJ3461] Wu Bo, 1570poj3461

★☆Input file:oulipo.inOutput file:oulipo.outSimple comparison
Time Limit: 1 s memory limit: 256 MB

[Description]

The French writer Georges Perec 1936-1982 once wrote a book, "La disparition", without a letter 'E '. He is a member of the Oulipo Group. Below is a passage in his book:

 

Tout avait Pair normal, mais tout s 'affirmait faux. tout avait Fair normal, d 'abord, puis surgissait l 'inhumain, l' affolant. il aurait voulu savoir o locale s 'articulait l' association qui l 'unissait au roman: stir son tapis, assaillant à tout instant son imagination, l 'intuition d' un tabou, la vision d 'un mal obscur, d' un quoi vacant, d' un non-dit: la vision, l 'avision d' un oubli commandant tout, o ù s' Abolissait la raison: tout avait l 'air normal mais...

 

Perec is likely to score a high score (or a low score, of course) in the following competitions ). In this competition, people are asked to write or even meaningful articles on a topic, and make a given "word" appear as few times as possible. Our task is to write a program for the Panel to count the number of times that the word appears, to get the final ranking of contestants. Contestants often write a long string of nonsense, such as 500000 consecutive't '. And they do not need spaces.

Therefore, we want to find the frequency of a word as soon as possible, that is, the number of times a given string appears in the article. More formally, the alphabet {'A', 'B', 'C ',..., 'Z'} and two finite strings consisting of only letters in the alphabet: W and T. Find the number of times W appears in T. "Appear" indicates that all consecutive characters in W must correspond to consecutive characters in T. The two W in T may partially overlap.

[Input format]

The input contains multiple groups of data.

The first line of the input file has an integer representing the number of data groups. The following figure shows the data in the following format:

The first line is the word W, which consists of {'A', 'B', 'C ',..., A string consisting of letters in 'Z'}, which must be 1 <=| W |<= 10000 (| W | represents the length of string W)

The second line is article T, which consists of {'A', 'B', 'C ',..., A string consisting of letters in 'Z'}. | W |<=| T |<= 1000000.

[Output format]

Output an integer for each data group, that is, the number of occurrences of W in T.

[Example input]

3

BAPC

BAPC

AZA

AZAZAZA

VERDI

AVERDXIVYERDIAN

[Sample output]

1

3

0

[Source]

BAPC 2006 Qualification

POJ 3461

 

 

KMP bare question,

However, you can use string hash,

For word a, article B

First obtain the hash value of word a, and then obtain the hash value of each bit of word B,

Then enumerate every bit of article B and check whether the interval is valid.

 

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<cstring> 6 #include<algorithm> 7 #include<map> 8 #define lli long long int  9 #define ull unsigned long long 10 using namespace std;11 const ull MAXN=1000001;12 ull base=27;13 void read(ull &n)14 {15     char c='+';ull x=0;bool flag=0;16     while(c<'0'||c>'9'){c=getchar();if(c=='-')flag=1;}17     while(c>='0'&&c<='9')x=x*10+c-48,c=getchar();18     n=flag==1?-x:x;19 }20 ull Pow[MAXN];21 ull ha[MAXN];22 ull pd(ull l,ull r){return (ull)ha[r]-Pow[r-l+1]*ha[l-1];}23 char a[MAXN];24 char b[MAXN];25 int main()26 {27     freopen("oulipo.in","r",stdin);28     freopen("oulipo.out","w",stdout);29     ull n;read(n);30     Pow[0]=1;31     for(ull i=1;i<=MAXN;i++)32         Pow[i]=Pow[i-1]*base;33     while(n--)34     {35         int ans=0;36         scanf("%s%s",a+1,b+1);37         int la=strlen(a+1);ull lb=strlen(b+1);38         ull hasha=0;39         for(ull i=1;i<=la;i++)40             hasha=hasha*base+a[i];41         for(ull i=1;i<=lb;i++)42             ha[i]=ha[i-1]*base+b[i];43         for(ull i=1;i<=lb-la+1;i++)44         {45             ull k=pd(i,i+la-1);46             if(k==hasha)47                 ans++;48         }49             50         printf("%d\n",ans);51     }52     return 0;53 }

 

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.