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;
}

最新文章

  1. mysql编码格式设置
  2. thinkPHP(待更新)
  3. POJ 2051
  4. hadoop历史服务器配置问题
  5. 为什么要用专业的ETL
  6. codeforces 235 B. Let&#39;s Play Osu!
  7. 【JQuery Plugin】WdatePicker
  8. Installshield脚本拷贝文件常见问题汇总
  9. hdu1083二分图匹配模板题
  10. axis1.4开发webservice客户端(快速入门)-基于jdk1.4
  11. EMM386和UMBPCI 区别
  12. js 一个对象的属性名是一个变量怎么处理?
  13. Ubuntu16.04下搭建Go语言环境
  14. U盘复制文件到最后5秒会卡住怎么办解决
  15. datatable to entiy list 不支持可空类型和枚举类型
  16. SVN和IntelliJ IDEA忽略node_module设置
  17. ios - 设置一个VC的navigationController的显示图案或文字,其他navigationController依旧不变
  18. HDU 4272 LianLianKan (状压DP+DFS)题解
  19. java解析json串获取key和value
  20. 一图看懂hadoop Yarn工作原理

热门文章

  1. vi和vim上查找字符串
  2. win8系统 如何默认显示文件扩展名和显示隐藏文件
  3. java notify notifyAll
  4. BZOJ 1122 POI2008 账本BBB 单调队列
  5. jQuery操作Form表单元素
  6. android真机调试 INSTALL_FAILED_MEDIA_UNAVAILABLE 问题解决方案
  7. 嵌入式开发之davinci---IPIPE、IPIPEIF and ISIF这三者有什么区别
  8. POJ 1125 Stockbroker Grapevine (Floyd最短路)
  9. JSON: Circular Dependency Errors
  10. c# 字节高低位