【codeforces #282(div 1)】AB题解
2 seconds
256 megabytes
standard input
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.
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.
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.
(((#)((#)
1
2
()((#((#(#()
2
2
1
#
-1
(#)
-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;
}
2 seconds
256 megabytes
standard input
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 consists of two lines containing strings s and t (1 ≤ |s|, |t| ≤ 105).
Each string consists of lowercase Latin letters.
Print the answer in a single line.
ababa
aba
5
welcometoroundtwohundredandeightytwo
d
274201
ddd
d
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 #282(div 1)】AB题解的更多相关文章
- Codeforces #282 div 1 C Helping People 题解
CF 282 C Helping People 题解 [原题] time limit per test 2 seconds memory limit per test 512 megabytes in ...
- [Codeforces] #603 (Div. 2) A-E题解
[Codeforces]1263A Sweet Problem [Codeforces]1263B PIN Code [Codeforces]1263C Everyone is a Winner! [ ...
- # Codeforces Round #529(Div.3)个人题解
Codeforces Round #529(Div.3)个人题解 前言: 闲来无事补了前天的cf,想着最近刷题有点点怠惰,就直接一场cf一场cf的刷算了,以后的题解也都会以每场的形式写出来 A. Re ...
- Codeforces Round #557 (Div. 1) 简要题解
Codeforces Round #557 (Div. 1) 简要题解 codeforces A. Hide and Seek 枚举起始位置\(a\),如果\(a\)未在序列中出现,则对答案有\(2\ ...
- Codeforces #344 Div.2
Codeforces #344 Div.2 Interview 题目描述:求两个序列的子序列或操作的和的最大值 solution 签到题 时间复杂度:\(O(n^2)\) Print Check 题目 ...
- Codeforces #345 Div.1
Codeforces #345 Div.1 打CF有助于提高做题的正确率. Watchmen 题目描述:求欧拉距离等于曼哈顿距离的点对个数. solution 签到题,其实就是求有多少对点在同一行或同 ...
- Codeforces Beta Round #27 (Codeforces format, Div. 2)
Codeforces Beta Round #27 (Codeforces format, Div. 2) http://codeforces.com/contest/27 A #include< ...
- Codeforces#441 Div.2 四小题
Codeforces#441 Div.2 四小题 链接 A. Trip For Meal 小熊维尼喜欢吃蜂蜜.他每天要在朋友家享用N次蜂蜜 , 朋友A到B家的距离是 a ,A到C家的距离是b ,B到C ...
- codeforces #592(Div.2)
codeforces #592(Div.2) A Pens and Pencils Tomorrow is a difficult day for Polycarp: he has to attend ...
随机推荐
- 【bzoj4282】慎二的随机数列
扯几句题外的,最近在看Fate/StayNight,对此人毫无好感…… 每次减一下当前可辨认数,然后随意dp一个LIS,最后记得加回去就好. #include<bits/stdc++.h> ...
- linux环境下,双击直连ping私有地址时候出现Destination host unreachable 解决办法
在确保网线无故障的情况下,采取以下步骤 1.查看本机的hostname vim /etc/sysconfig/network 2.编辑/etc/hosts vim /etc/hosts 加入以下 ...
- 病毒&烦人的幻灯片
<病毒>传送门 <烦人的幻灯片>传送门 病毒 描述 有一天,小y突然发现自己的计算机感染了一种病毒!还好,小y发现这种病毒很弱,只是会把文档中的所有字母替换成其它字母,但并不改 ...
- Selenium2+python自动化73-定位的坑:class属性有空格【转载】
前言 有些class属性中间有空格,如果直接复制过来定位是会报错的InvalidSelectorException: Message: The given selector u-label f-dn ...
- SpringMVC框架入门配置 IDEA下搭建Maven项目(zz)
SpringMVC框架入门配置 IDEA下搭建Maven项目 这个不错哦 http://www.cnblogs.com/qixiaoyizhan/p/5819392.html
- Photoshop CC 2015
1.Ctrl 加 + -号实现放大缩小2.Ctrl+T 自由变换3.Ctrl+D 取消选区,再选择处理完后想要选中别的区域的话要取消之前选中的区域,要不然之前的区域一直都是选中状态4.F12 将文件恢 ...
- 《锋利的jQuery》读书要点笔记6——制作商城网页:结构和样式设计
第8章 用jQuery打造个性网站 做一个购物网站并用jQuery来完善.大体步骤是: 收集素材 确定网站结构 A. 文件结构,imagea文件夹用来存放将要用到的图片,styles文件夹存放CSS, ...
- 两个sql设计方案的比较
我有一个买方表Buyer,大概1万条记录:一个卖方表Sale,大概5万条记录.有一些买方和卖方之间是有某种关联的,这种关联关系被记录在Partner表里,Partner表中的关键字段包括BuyerID ...
- hdu 1422(贪心)
重温世界杯 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submiss ...
- 在WINDOWS2008 Server 中创建NFS服务器,使用LINUX的MOUNT命令去加载网络盘
1.在WINDOWS服务器中创建NFS SERVER 首先,打开服务管理器,选择添加角色: 选中文件服务,下一步: 出现一个提示,不管它,继续下一步: 在接下来的页面中选中“网 ...