Another Meaning

链接:http://acm.hust.edu.cn/vjudge/problem/435534/origin


题目: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.

题意:字符串a中出现的b可以被修改(理解成换成*比较方便),当然可以不修改。问共有多少种方案。

分析:一开始想用贪心的,但是情况太复杂(出现b的一部分和a链接后还可以匹配等)不太好讨论,所以采用dp,把问题分解成从1到n,n《=a的长度,的字符可以有多少方案,再之后的b的长度的字符串是否可以替换,可以就是(n+len(b)-1)位的种数(因为长度不够,没达到替换要求,视为不替换的方案数)加上替换后的n的种数(替换为*)。最后dp【len(a)】即为结果。(对了,由于是字串是动态的,不能用kmp优化,要用哈希来优化,不知道暴力匹配会不会超时。。。。)


题解:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <functional>
#include <cmath>
#include <cctype>
#include <cfloat>
#include <climits>
#include <complex>
#include <deque>
#include <list>
#include <set>
#include <utility>
#define rt return
#define fr freopen("in.txt","r",stdin)
#define fw freopen("out.txt","w",stdout)
#define ll long long
#define ull unsigned long long
#define detie ios_base::sync_with_stdio(false);cin.tie(false);cout.tie(false)
#define pii pair<int,int>
#define lowbit(x) x&(-x)
using namespace std;
#define maxi 0x3f3f3f3f
#define MAX 100020

int t, dp[MAX], al, bl, cases;
char a[MAX], b[MAX];
ull B = 100000007;
ull MOD = 1000000007;
int main()
{
	//fr;
	detie;
	cin >> t;
	while (t--)
	{
		cin >> a >> b;
		al = strlen(a);
		bl = strlen(b);
		ull ah = 0, bh = 0, t = 1;
		for (int i = 0; i < bl; i++)
		{
			t *= B;
			ah = ah*B + a[i];
			bh = bh*B + b[i];
		}
		memset(dp, 0, sizeof dp);
		fill(dp, dp + bl, 1);
		for (int i = 0; i + bl <= al; i++)
		{
			dp[i + bl] = dp[i + bl - 1];
			if (ah == bh)dp[i + bl] = (dp[i + bl] + dp[i]) % MOD;
			if (i + bl < al)
				ah = ah*B + a[i + bl] - t*a[i];
		}
		printf("Case #%d: %d\n", ++cases, dp[al]);
	}
	rt 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值