zoukankan      html  css  js  c++  java
  • 【47.92%】【hdu 5763】Another Meaning

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 1440    Accepted Submission(s): 690


    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”.
     

    Author
    FZU
     

    Source
     

    【题解】

    设c[i]表示前i个字符能组成多少种不同意思。

    c[0..lens]初值为1;

    c[i] += c[i-1]-1 (不取其特殊意思);

    c[i] += c[i-lenb] (如果存在。则取其特殊意思);

    abcdefgh

    efgh

    对于这样的输入

    c[4]代表的是不取特殊意思的。==1

    然后从c[4]可以推到c[4+lenb]

    即c[4+lenb]+=c[4];

    可以理解为

    abcd****

    abcdefgh这两种。

    至于怎么取一个合适的位置做这样的推导。KMP

    【代码】

    #include <cstdio>
    #include <cstring>
    
    const int MAX_SIZE = 101000;
    const int MOD = 1000000007;
    
    int t;
    int f[MAX_SIZE],c[MAX_SIZE];
    char  s[MAX_SIZE], p[MAX_SIZE];
    bool can[MAX_SIZE];
    int lens, lenp;
    
    void init()
    {
    	memset(f, 0, sizeof(f));
    	memset(c, 0, sizeof(c));
    	memset(can, false, sizeof(can));
    }
    
    void input_data()
    {
    	scanf("%s", s);
    	scanf("%s", p);
    	f[0] = 0; f[1] = 0;
    	lens = strlen(s);lenp = strlen(p);
    	for (int i = 1; i <= lenp - 1; i++)
    	{
    		int j = f[i];
    		while (j && (p[i] != p[j])) j = f[j];
    		f[i + 1] = (p[j] == p[i] ? j + 1 : 0);
    	}
    	int j = 0;
    	for (int i = 0; i <= lens - 1; i++)
    	{
    		while (j && (p[j] != s[i]))j = f[j];
    		if (p[j] == s[i]) j++;
    		if (j == lenp)
    			can[i - lenp + 1] = true;
    	}
    }
    
    void get_ans()
    {
    	for (int i = 0; i <= lens; i++)
    		c[i] = 1;
    	for (int i = 0; i <= lens; i++)
    	{
    		if (i)
    			c[i] = (c[i] + c[i - 1] - 1) % MOD;
    		if (can[i])
    			c[i + lenp] = (c[i+lenp] + c[i]) % MOD;
    	}
    }
    
    void output_ans()
    {
    	printf("%d
    ", c[lens]);
    }
    
    int main()
    {
    	//freopen("F:\rush.txt", "r", stdin);
    	scanf("%d", &t);
    	for (int i = 1; i <= t; i++)
    	{
    		printf("Case #%d: ", i);
    		init();
    		input_data();
    		get_ans();
    		output_ans();
    	}
    	return 0;
    }


  • 相关阅读:
    洛谷P2878 [USACO07JAN]保护花朵Protecting the Flowers
    洛谷P2434 [SDOI2005]区间
    洛谷 P2701 [USACO5.3]巨大的牛棚Big Barn
    洛谷 P2904 [USACO08MAR]跨河River Crossing
    Again Stone Game
    Crazy Calendar (阶梯博弈变形)
    (2017浙江省赛E)Seven Segment Display
    ZOJ 3705 Applications
    D
    The Suspects(并查集维护根节点信息)
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7632243.html
Copyright © 2011-2022 走看看