[Usaco2012 Nov]Concurrently Balanced Strings
Description
[Brian Dean, 2012] Farmer John's cows are all of a very peculiar breed known for its distinctive appearance -- each cow is marked with a giant spot on its hide in the shape of a parenthesis (depending on the direction the cow is facing, this could look like either a left or a right parenthesis). One morning, Farmer John arranges his cows into K lines each of N cows (1 <= K <= 10, 1 <= N <= 50,000). The cows are facing rather arbitrary directions, so this lineup can be described by K length-N strings of parentheses S_1,..., S_k. Farmer John notes with great excitement that some ranges of his cows are "concurrently balanced", where a range i...j of cows is concurrently balanced only if each of the strings S_1,..., S_k is balanced in that range (we define what it means for a single string of parentheses to be balanced below). For instance, if K = 3, and we have S_1 = )()((())))(()) S_2 = ()(()()()((()) S_3 = )))(()()))(()) 1111 01234567890123 Then the range [3...8] is concurrently balanced because S_1[3...8] = ((())), S_2[3...8] = ()()(), and S_3[3...8] = (()()). The ranges [10...13] and [11...12] are also concurrently balanced. Given K length-N strings of parentheses, help Farmer John count the number of pairs (i,j) such that the range i...j is concurrently balanced. There are several ways to define what it means for a single string of parentheses to be "balanced". Perhaps the simplest definition is that there must be the same total number of ('s and )'s, and for any prefix of the string, there must be at least as many ('s as )'s. For example, the following strings are all balanced: () (()) ()(()()) while these are not: )( ())( ((())))
Input
- Line 1: Two integers, K and N.
- Lines 2..K+1: Each line contains a length-N string of parentheses.
Output
- Line 1: A single integer, the number of concurrently balanced ranges.
Sample Input
3 14
)()((())))(())
()(()()()((())
)))(()()))(())
Sample Output
3
找到一些区间,使得这些字符串的区间都是合法括号序列
合法的括号序列肯定有\(sum[r]=sum[l]\),当我们枚举到一个i时,记录所有串的sum,多次访问时记录答案,类似于等差数列,然后记得特判一下非法情况
(map里面用vector映射一个pair还真是头一次见……)
/*problem from Wolfycz*/
#include<map>
#include<cmath>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define Fi first
#define Se second
#define MK make_pair
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
static char buf[1000000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
inline int frd(){
int x=0,f=1; char ch=gc();
for (;ch<'0'||ch>'9';ch=gc()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=gc()) x=(x<<3)+(x<<1)+ch-'0';
return x*f;
}
inline int read(){
int x=0,f=1; char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+ch-'0';
return x*f;
}
inline void print(int x){
if (x<0) putchar('-');
if (x>9) print(x/10);
putchar(x%10+'0');
}
const int N=5e4;
int v[15][N+10],f[15][(N<<1)+10];
char s[N+10];
map<vector<int>,pair<int,int> >Mp;
int main(){
int k=read(),n=read(),Ans=0;
for (int i=0;i<k;i++){
scanf("%s",s);
for (int j=0;j<n;j++) v[i][j]=(s[j]=='('?1:-1);
}
vector<int>vec(k,n);
for (int i=0;i<k;i++){
for (int j=0;j<=n<<1;j++) f[i][j]=n;
f[i][n]=0;
}
Mp.insert(map<vector<int>,pair<int,int> >::value_type(vec,MK(0,1)));
for (int i=0;i<n;i++){
int limit=0;
for (int j=0;j<k;j++){
if (v[j][i]==1) f[j][++vec[j]]=i+1;
else vec[j]--,f[j][vec[j]]=min(f[j][vec[j]],i+1);
limit=max(limit,f[j][vec[j]]);
}
if (limit==n) continue;
if (Mp.find(vec)==Mp.end()) Mp.insert(map<vector<int>,pair<int,int> >::value_type(vec,MK(0,0)));
pair<int,int>&tmp=Mp.find(vec)->Se;
if (tmp.Fi==limit) Ans+=tmp.Se++;
else tmp=MK(limit,1);
}
printf("%d\n",Ans);
return 0;
}
[Usaco2012 Nov]Concurrently Balanced Strings的更多相关文章
- [USACO12NOV]同时平衡线Concurrently Balanced Strings DP map 思维
题面 [USACO12NOV]同时平衡线Concurrently Balanced Strings 题解 考虑DP. \(f[i]\)表示以\(i\)为左端点的合法区间个数.令\(pos[i]\)表示 ...
- BZOJ3016: [Usaco2012 Nov]Clumsy Cows
3016: [Usaco2012 Nov]Clumsy Cows Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 71 Solved: 52[Submi ...
- 3016: [Usaco2012 Nov]Clumsy Cows
3016: [Usaco2012 Nov]Clumsy Cows Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 91 Solved: 69[Submi ...
- 3018: [Usaco2012 Nov]Distant Pastures
3018: [Usaco2012 Nov]Distant Pastures Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 43 Solved: 20[ ...
- [LeetCode]1221. Split a String in Balanced Strings
Balanced strings are those who have equal quantity of 'L' and 'R' characters. Given a balanced strin ...
- 【leetcode】1221. Split a String in Balanced Strings
题目如下: Balanced strings are those who have equal quantity of 'L' and 'R' characters. Given a balanced ...
- 【LeetCode】1221. Split a String in Balanced Strings 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计 日期 题目地址:https://leetcode ...
- 【BZOJ】3016: [Usaco2012 Nov]Clumsy Cows(贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=3016 之前yy了一个贪心,,,但是错了,,就是枚举前后对应的字符(前面第i个和后面第i个)然后相同答 ...
- BZOJ 3016 [Usaco2012 Nov]Clumsy Cows:贪心
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3016 题意: 给你一个括号序列,问你至少修改多少个括号,才能使这个括号序列合法. 题解: ...
随机推荐
- javascript实现 京东淘宝等商城的商品图片大图预览功能(图片放大器)
在京东和淘宝等购买东西的时候,我们会经常预览左侧商品展示图片,把鼠标放到原图,右侧就会有个大图显示出细节.本文将带领大家写一个这样简单的功能! 一.实现原理 当鼠标移入某一图片内部时,图片上部会出 ...
- linux迁移至固态硬盘全过程
自从台式机上用上固态硬盘后,就再也受不了笔记本上的5400转的机械硬盘了,所以这次又买了块固态硬盘打算装到笔记本上. 笔记本里装的是Ubuntu 14.04 + Win7双系统,Win7主要偶尔运行一 ...
- SharePoint 2013 调查问卷的使用方法
SharePoint 2013 调查问卷的使用方法 1,介绍调查问卷的用法. 2.图形和全部结果. 3,控制用户仅仅能看到自己答案. 1.确认有权限,假设没有管理管理权限请向管理员申请. 站点&quo ...
- asp.net mvc 抓取京东商城分类
555 asp.net mvc 抓取京东商城分类 URL:http://www.jd.com/allSort.aspx 效果: //后台代码 public ActionResult Get ...
- Selenium系列之--02 不同浏览器获取Xpath的方法
一.Chrome浏览器 1.1 获取XPath 1. 使用浏览器打开需测试的网址,然后点击[F12]按钮,打开开发者调试工具: 2. 点击开发者工具中第一行的第一个对话框Elements,这时就看 ...
- iOS项目开发实战——plist数组解析
plist数据是苹果公司创造的数据格式,基于XML,因为在iOS,Mac系统中操作plist很方便,所以我们经常会用到.在iOS项目中.系统会自己主动生成一个Info.plist文件,里面存放了iOS ...
- java纯数字加密解密实例
我们都知道,在用户加入信息时,一些比較敏感的信息,如身份证号,手机号,用户的登录password等信息,是不能直接明文存进数据库的.今天我们就以一个详细的样例来说明一下纯数字的java加密解密技术. ...
- 使用JDBC 插入向数据库插入对象
package com.ctl.util; import java.io.IOException; import java.lang.reflect.Field; import java.lang.r ...
- Linux下使用inotify实现对文件的监控
项目中,要实现用户通过网页设置參数,后台接收数据然后写串口. 网页写数据到本地文件,使用inotify监控文件的IN_MODIFY事件.当文件被改动,然后触发写串口事件. 第一个程序只把要监控的文件增 ...
- cygwin安装sshd服务(win7)Error installing a service: OpenSCManager: Win32 error 5:
Error installing a service: OpenSCManager: Win32 error 5: 出现这个问题的解决办法:win7系统管理员运行Cygwin软件 ...