2016暑假多校联合---Another Meaning

2016暑假多校联合---Another Meaning

Problem Description

As is known to all, in many cases, a word has two 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 ??. ?? is so smart that he knows the word B in the sentence has two meanings. He wants to know how many kinds of meanings MeiZi can express.

Input

The first line of the input gives the number of test cases T; T test cases follow.
Each test case contains two strings A and B, A means the sentence MeiZi sends to ??, B means the word B which has two menaings. string only contains lowercase letters.

Limits
T <= 30
|A| <= 100000
|B| <= |A|

Output

For each test case, output one line containing “Case #x: y” (without quotes) , where x is the test case number (starting from 1) and y is the number of the different meaning of this sentence may be. Since this number may be quite large, you should output the answer modulo 1000000007.

Sample Input

4

hehehe

hehe

woquxizaolehehe

woquxizaole

hehehehe

hehe

owoadiuhzgneninougur

iehiehieh

Sample Output

Case #1: 3

Case #2: 2

Case #3: 5

Case #4: 1

Hint

In the first case, “ hehehe” can have 3 meaings: “*he”, “he*”, “hehehe”.
In the third case, “hehehehe” can have 5 meaings: “*hehe”, “he*he”, “hehe*”, “**”, “hehehehe”.

思路:递推(DP) 当前位置以前的语句的含义种数为包含当前多语义与不包含;

代码如下:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const long long mod=1e9+7;

char s[100005];
char ss[100005];
int next1[100005];
long long num[100005];
int       pos[100005];

void makenext1(const char P[])
{
    int q,k;
    int m = strlen(P);
    next1[0]=0;
    for (q = 1,k = 0; q < m; ++q)
    {
        while(k > 0 && P[q] != P[k])
            k = next1[k-1];
        if (P[q] == P[k])
        {
            k++;
        }
        next1[q] = k;
    }
}

long long calc(char T[],char P[])
{
    int n,m;
    int i,q;
    int tot=0;
    n = strlen(T);
    m = strlen(P);
    makenext1(P);
    for(i=0,q = 0; i < n; ++i)
    {
        while(q>0&&P[q]!=T[i])
            q=next1[q-1];
        if(P[q]==T[i])
        {
            q++;
        }
        if(q==m)
        {
            long long flag=1;
            pos[tot]=i-m+1;
            if(tot>0)
            {
                if(pos[tot-1]+m<=pos[tot])
                {
                    num[tot]=(2*num[tot-1])%mod;
                }
                else
                {
                    num[tot]=num[tot-1]%mod;
                    for(int h=tot-2;h>=0;h--)
                   {
                       if(pos[h]+m<=pos[tot])
                      {
                         num[tot]=(num[tot]+num[h])%mod;
                         flag=0;  ///当之前不存在不相重叠的语句时;
                         break;
                      }
                   }
                   num[tot]=(num[tot]+flag)%mod;
                }
            }
            else
            {
                num[tot]=2;
            }
            tot++;
         }
    }
    if(tot==0) return 1;
    return num[tot-1];
}

int main()
{
    int T;
    int Case=1;
    cin>>T;
    while(T--)
    {
        scanf("%s%s",s,ss);
        printf("Case #%d: %lld\n",Case++,calc(s,ss));
       // cout<<(calc(s,ss)%mod+mod)%mod<<endl;
    }
    return 0;
}
时间: 2024-08-03 07:29:10

2016暑假多校联合---Another Meaning的相关文章

2016暑假多校联合---Windows 10

2016暑假多校联合---Windows 10(HDU:5802) Problem Description Long long ago, there was an old monk living on the top of a mountain. Recently, our old monk found the operating system of his computer was updating to windows 10 automatically and he even can't j

2016暑假多校联合---Substring(后缀数组)

2016暑假多校联合---Substring Problem Description ?? is practicing his program skill, and now he is given a string, he has to calculate the total number of its distinct substrings. But ?? thinks that is too easy, he wants to make this problem more interesti

2016暑假多校联合---Rikka with Sequence (线段树)

2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them: Yuta has an array A with n numbers. Then he make

2016暑假多校联合---Death Sequence(递推、前向星)

原题链接 Problem Description You may heard of the Joseph Problem, the story comes from a Jewish historian living in 1st century. He and his 40 comrade soldiers were trapped in a cave, the exit of which was blocked by Romans. They chose suicide over captu

2016暑假多校联合---Joint Stacks (STL)

HDU  5818 Problem Description A stack is a data structure in which all insertions and deletions of entries are made at one end, called the "top" of the stack. The last entry which is inserted is the first one that will be removed. In another wor

2016暑假多校联合---Counting Intersections

原题链接 Problem Description Given some segments which are paralleled to the coordinate axis. You need to count the number of their intersection. The input data guarantee that no two segments share the same endpoint, no covered segments, and no segments

2016暑假多校训练参赛感想

参赛感想 这是第一次参加暑假多校训练,应该也会是人生中最后一次,我真的很庆幸能参加这个训练,和全国几乎所有高校的ACMer一起在一个平台上做题!昨天为止多校已经完全结束,今天看到叉姐的训练感想(叉姐的感想链接),我觉得我也有必要写下自己的训练感想. 人的眼界总是狭窄的,当在自己的学校站在前几名的时候觉得自己还不错,应该会有不错的将来,但是当第一次参加国赛(2015 南阳站)的时候我便被别人实力所震撼,我突然觉得自己在别人的眼里简直就是小学生,菜到不行.别人在5个小时可以AK,而我连最水的题也要想

2015暑假多校联合---CRB and His Birthday(01背包)

题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5410 Problem Description Today is CRB's birthday. His mom decided to buy many presents for her lovely son.She went to the nearest shop with M Won(currency unit).At the shop, there are N kinds of pr

2015暑假多校联合---Expression(区间DP)

题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5396 Problem Description Teacher Mai has n numbers a1,a2,?,anand n−1 operators("+", "-" or "*")op1,op2,?,opn−1, which are arranged in the form a1 op1 a2 op2 a3 ? an. He wan