Hdu 5763 another meaning hash +dp

Source: Internet
Author: User

another meaning

Time limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 917 Accepted Submission (s): 434


problem Descriptionas is known to all, in many cases, a word has both meanings. Such as "hehe", which not only means "hehe", but also means "excuse me".
Today,? is chating with Meizi online, Meizi sends a sentence A to??.?? are so smart and he knows the word B in the Sentence has a meanings. He wants to know how many kinds of meanings meizi can express. 

InputThe first line of the input gives the number of test cases T; T test cases follow.
Each test case contains-strings A and B, a means the sentence Meizi sends to??, B means the word B-which has a Menaings. String only contains lowercase letters.

Limits
T <=
| a| <= 100000
| b| <= | a|

 

Outputfor each test case, output one line containing ' case #x: Y ' (without quotes), where x is the ' test Case number (Starti Ng from 1) and Y are the number of the different meaning of this sentence. Since This number is quite large, you should output the answer modulo 1000000007. 

Sample Input4hehehehehewoquxizaolehehewoquxizaoleheheheheheheowoadiuhzgneninouguriehiehieh 

Sample OutputCase #1:3 case #2:2 case #3:5 case #4:1 HintIn the first case , "hehehe" Can has 3 meaings: "*he", "he*", "hehehe". In the third case, "Hehehehe" Can has 5 meaings: "*hehe", "He*he", "hehe*", "* *", "hehehehe".

Test instructions: give you a substring of the main string, and then match the string to the substring to change the match to * (or not to change). Ask the main string how many clocks are different;

#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <vector > #include <queue> #include <cstring> #include <string> #include <algorithm>using namespace Std;typedef Long long ll;typedef unsigned long long ull; #define MM (A, B) memset (A,b,sizeof (a)); #define INF 0x7f7f7f7f#def ine for (i,n) for (int i=1;i<=n;i++) #define CT continue; #define PF printf#define SC scanfconst int Mod=1000000007;const i   NT N=1e5+10;int la,lb;ll dp[n];char a[n],b[n];ull ha[n],hb;ull seed=13331;void init () {hb=0;   for (int i=0;i<lb;i++) hb=hb*seed+b[i];   Ull base=1;   for (int i=1;i<=lb-1;i++) base*=seed;   HA[0]=A[0];   for (int i=1;i<=lb-1;i++) ha[i]=ha[i-1]*seed+a[i]; for (int i=lb;i<la;i++) ha[i]= (ha[i-1]-a[i-1-(lb-1)]*base) *seed+a[i];}    int main () {int cas,kk=0;    scanf ("%d", &cas);        while (cas--) {scanf ("%s", a);        scanf ("%s", b);        La=strlen (a); Lb=strlen (b); if (la<lb) {PRintf ("Case #%d:1\n", ++KK);        CT;}        Init ();        for (int i=0;i<=lb-1;i++) dp[i]=1;        if (HA[LB-1]==HB) dp[lb-1]=2;           for (int i=lb;i<la;i++) {dp[i]=dp[i-1]%mod;        if (HA[I]==HB) dp[i]= (dp[i]+dp[i-lb])%mod;    } printf ("Case #%d:%lld\n", ++kk,dp[la-1]%mod); } return 0;}

Analysis: Error point:

1.BKDRhash is not very skilled, only the original final output of the form of &, resulting in the final computational complexity;

Improved: Bkdrhash form: Pre*seed+a[i],seed is a large prime number such as 13331, pre is the hash value before I;

A[i] is the character

2, do not think of DP, current subscript i, dp[i] value, if the current I do not match to the substring, dp[i]=dp[i-1];

If matched to a substring, dp[i]=dp[i-1]+dp[i-lb];

Hdu 5763 another meaning hash +dp

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.