【codeforces #282(div 1)】AB题解

本文深入探讨了两个编程难题:一是通过替换特殊字符来解开宝藏之门,二是计算特定字符串在另一字符串中非重叠子串出现的次数。文章详细介绍了解决方案,并提供了代码实现,旨在帮助读者理解贪心算法和动态规划等核心概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A. Treasure
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Malek has recently found a treasure map. While he was looking for a treasure he found a locked door. There was a string s written on the door consisting of characters '(', ')' and '#'. Below there was a manual on how to open the door. After spending a long time Malek managed to decode the manual and found out that the goal is to replace each '#' with one or more ')' characters so that the final string becomes beautiful.

Below there was also written that a string is called beautiful if for each i (1 ≤ i ≤ |s|) there are no more ')' characters than '(' characters among the first i characters of s and also the total number of '(' characters is equal to the total number of ')' characters.

Help Malek open the door by telling him for each '#' character how many ')' characters he must replace it with.

Input

The first line of the input contains a string s (1 ≤ |s| ≤ 105). Each character of this string is one of the characters '(', ')' or '#'. It is guaranteed that s contains at least one '#' character.

Output

If there is no way of replacing '#' characters which leads to a beautiful string print  - 1. Otherwise for each character '#' print a separate line containing a positive integer, the number of ')' characters this character must be replaced with.

If there are several possible answers, you may output any of them.

Sample test(s)
input
(((#)((#)
output
1
2
input
()((#((#(#()
output
2
2
1
input
#
output
-1
input
(#)
output
-1


贪心。


(表示1,)表示-1。


满足条件则前缀和时刻都要>=0。


那么遇到#我们只让他表示一个),前缀和只-1,遇到最后一个#再把前面的债还清。


一开始WA了,因为我遇到最后一个#就不管前缀和了。。


因此(#(这样的数据就过不了。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#define M 100000+5
using namespace std;
char s[M];
int ans[M];
int main()
{
        scanf("%s",s);
	int l=strlen(s);
	int la,now=0,tot=0;
	for (int i=0;i<l;i++)
	{
	    if (s[i]=='#')
		{
			ans[++tot]=1;
			now--;
			la=i;
		}
		if (s[i]=='(') now++;
		if (s[i]==')') now--;
		if (now<0)
		{
			puts("-1");
			return 0;
		}
	}
	int x=0;
	for (int i=l-1;i>la;i--)
	{
		if (s[i]=='(')
			x--;
	    else x++;
	    if (x<0)
	    {
			puts("-1");
		    return 0;
	    }
	}
	ans[tot]+=now;
	for (int i=1;i<=tot;i++)
		printf("%d\n",ans[i]);
	return 0;
}

B. Obsessive String
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Hamed has recently found a string t and suddenly became quite fond of it. He spent several days trying to find all occurrences of t in other strings he had. Finally he became tired and started thinking about the following problem. Given a string s how many ways are there to extract k ≥ 1 non-overlapping substrings from it such that each of them contains string t as a substring? More formally, you need to calculate the number of ways to choose two sequences a1, a2, ..., ak and b1, b2, ..., bk satisfying the following requirements:

  • k ≥ 1
  •   t is a substring of string saisai + 1... sbi (string s is considered as 1-indexed).

As the number of ways can be rather large print it modulo 109 + 7.

Input

Input consists of two lines containing strings s and t (1 ≤ |s|, |t| ≤ 105). Each string consists of lowercase Latin letters.

Output

Print the answer in a single line.

Sample test(s)
input
ababa
aba
output
5
input
welcometoroundtwohundredandeightytwo
d
output
274201
input
ddd
d
output
12

kmp+dp。


首先用kmp快速求出每一位的ok[i],也就是从i到ok[i]包含t,且ok[i]最小。


然后进行dp:

f[i]表示a[1]=i的方案数,这显然要倒着做。


f[i]=sigma(sigma(f[ok[i]+1...n-1)+sigma(f[ok[i]+2...n-1]...+f[n-1]))


维护后缀和sum[i]表示i到n-1的f值得后缀和。


再维护后缀和的后缀和ss[i]表示i到n-1的sum[i]的后缀和。


于是f[i]=ss[ok[i]+1],转移变成O(1)了!

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#define mod 1000000007
#define M 100000+5
using namespace std;
int ss[M],ne[M],n,m,ok[M],sum[M],f[M];
char s[M],t[M];
void Getfail()
{
	ne[0]=0;
	ne[1]=0;
	for (int i=1;i<m;i++)
	{
		int j=ne[i];
		while (j&&t[i]!=t[j])
			j=ne[j];
		ne[i+1]=t[i]==t[j]?j+1:0;
	}
}
void Find()
{
	Getfail();
	int j=0;
	int now=0;
	for (int i=0;i<n;i++)
		ok[i]=n;
	for (int i=0;i<n;i++)
	{
		while (j&&t[j]!=s[i])
			j=ne[j];
		if (t[j]==s[i])
			j++;
		if (j==m)
		{
			for (int k=now;k<=i-m+1;k++)
				ok[k]=min(i,ok[k]);
			now=i-m+2;
			j=ne[j];
		}
	}
}
int main()
{
        scanf("%s",s);
	scanf("%s",t);
	n=strlen(s),m=strlen(t);
	Find();
	for (int i=n-1;i>=0;i--)
	{
		f[i]=n-1-(ok[i]-1);
	    f[i]=(f[i]+ss[ok[i]+1])%mod;
		sum[i]=(sum[i+1]+f[i])%mod;
		ss[i]=(ss[i+1]+sum[i])%mod;
	}
	cout<<sum[0]%mod<<endl;
	return 0;
}


### 关于 Codeforces Round 839 Div 3 的题目与解答 #### 题目概述 Codeforces Round 839 Div 3 是一场面向不同编程水平参赛者的竞赛活动。这类比赛通常包含多个难度层次分明的问题,旨在测试选手的基础算法知识以及解决问题的能力。 对于特定的比赛问题及其解决方案,虽然没有直接提及 Codeforces Round 839 Div 3 的具体细节[^1],但是可以根据以往类似的赛事结构来推测该轮次可能涉及的内容类型: - **输入处理**:给定一组参数作为输入条件,这些参数定义了待解决的任务范围。 - **逻辑实现**:基于输入构建满足一定约束条件的结果集。 - **输出格式化**:按照指定的方式呈现最终答案。 考虑到提供的参考资料中提到的其他几场赛事的信息[^2][^3],可以推断出 Codeforces 圆桌会议的一般模式是围绕着组合数学、图论、动态规划等领域展开挑战性的编程任务。 #### 示例解析 以一个假设的例子说明如何应对此类竞赛中的一个问题。假设有如下描述的一个简单排列生成问题: > 对于每一个测试案例,输出一个符合条件的排列——即一系列数字组成的集合。如果有多种可行方案,则任选其一给出即可。 针对上述要求的一种潜在解法可能是通过随机打乱顺序的方式来获得不同的合法排列形式之一。下面是一个 Python 实现示例: ```python import random def generate_permutation(n, m, k): # 创建初始序列 sequence = list(range(1, n + 1)) # 执行洗牌操作得到新的排列 random.shuffle(sequence) return " ".join(map(str, sequence[:k])) # 测试函数调用 print(generate_permutation(5, 2, 5)) # 输出类似于 "4 1 5 2 3" ``` 此代码片段展示了怎样创建并返回一个长度为 `k` 的随机整数列表,其中元素取自 `[1..n]` 这个区间内,并且保证所有成员都是唯一的。需要注意的是,在实际比赛中应当仔细阅读官方文档所提供的精确规格说明,因为这里仅提供了一个简化版的方法用于解释概念。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值