2018.9.9 nowcoder 普及组第一场

C-括号

题目大意:一个只包含左右括号的字符串\(S\),希望删掉S中若干个字符,使得剩下的字符串是一个合法的括号串,有多少不同的方案。

Solution

  • 状态:\(f[i][j]\)表示处理到字符串的第\(i\)个位置,现在有\(j\)个左括号没有GF右括号的保留情况总方案数
  • 转移:见代码

Code

#include <iostream>
#include <cstdio>
#include <algorithm>
#define pf(x) printf("%d\n", x) const int N = 1e4 + 10;
const int mod = 1e9 + 7; int n;
char s[N];
int a[N], f[3][N]; int main(){
scanf("%d", &n);
scanf("%s", s + 1);
int now = 0;
f[now][0] = 1;
for (int i = 1; i <= n; i++){
now ^= 1;
if(s[i] == '('){
f[now][0] = f[now ^ 1][0] % mod;//继承上一个
for(int j = 1; j <= n; ++j)
f[now][j] = (f[now ^ 1][j - 1] + f[now ^ 1][j]) % mod;//现在是左括号,可以不选这个左括号,也可以选这个左括号,
} else if(s[i] == ')'){
f[now][n] = f[now ^ 1][n];//继承上一个
for(int j = 0; j < n; ++j)
f[now][j] = (f[now ^ 1][j + 1] + f[now ^ 1][j]) % mod;//现在是右括号,可以不选这个右括号, 但是选了就要减去一个左括号,于是要由上一个位置比现在多一个左括号转移来
}
}
printf("%d", (f[now][0] - 1 + mod ) % mod);//会有全为空的情况
return 0;
}

D-配对

题目大意:给出\(n\)个长度为\(l\)的字符串,只包含\(a\)到\(h\)八个字母,可以最多用\(k\)次操作声明两个字母等价,且等价具有传递性,求最多可以让多少对字符串完全等价

Solution

因为只有8个字符,所以可以将字符间的情况看作联通块,联通块的情况有\(\Large\{^{n}_{m}\}\)种,也就是把\(8\)个字母划分为\(8-k\)个非空子集中的个数,为第二类斯特林数

通过枚举字母的联通快情况,我们算出\(hash\)值来判断共有多少字符串等价

可以用随机数来\(hash\),因为不需要求区间\(hash\)值,而且不容易被卡.

但是如果每次都\(O(n\cdot l)\)的求所有的hash值,很容易超时,所以我们试着去简化,预处理出每一个字符串中每一种字母的累加哈希值\(\sum\limits_{s_i=c}base^i(has[i])\)

所以我们只需要处理出对于每个字符串的每个联通块的\(hash\)就好,可以去累加联通块中的每个字符的哈希值

Example

如果\(aabb \\ abbb\) 那么 \(has[1] = \alpha, has[2] = \beta, has[3] = \gamma,has[4]=\delta\)

\(inc[1][a] = \alpha \otimes \beta,inc[1][b] = \gamma \otimes \delta\)

\(inc[1][a] = \alpha,inc[1][b]=\beta \otimes \gamma \otimes \delta\)

假使\(a = b\),那么\(bel[a] = bel[b] = 1\)

\(nw[1][1] = \alpha \otimes \beta \otimes \gamma \otimes \delta,nw[2][1] = \alpha \otimes \beta \otimes \gamma \otimes \delta\)

Code

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring> typedef long long ll; const int N = 1005; int n, l, k;
ll has[N], inc[105][10], bel[10], nw[105][10];
char s[105][1005];
int ord[N]; inline ll Ra(){
return rand() * rand() * 1LL * rand();
} inline bool cmp(int a, int b){
for(int i = 1; i <= k; ++i)
if(nw[a][i] < nw[b][i]) return 1;
else if(nw[a][i] > nw[b][i]) return 0;
return 0;
} inline int check(){ for(int i = 1; i <= n; ++i){
memset(nw[i], 0, sizeof(nw[i]));
for(int j = 1; j <= 8; ++j){
nw[i][bel[j]] ^= inc[i][j];
}
ord[i] = i;
}
std::sort(ord + 1, ord + n + 1, cmp);
int cnt = 1, ans = 0;
for(int i = 2; i <= n; ++i){
if(!cmp(ord[i], ord[i - 1]) && !cmp(ord[i - 1], ord[i])) cnt++;
else ans += cnt * (cnt - 1) / 2, cnt = 1;//cnt要为1,因为这是记录对数
}
ans += cnt * (cnt - 1) / 2;
return ans;
} int ans = 0;
void dfs(int x, int tot){//tot表示联通块个数, x表示第x个字符
if(x >= 8){
if(tot != k) return ;
ans = std::max(ans, check());
return;
}
for(int i = 1; i <= tot; ++i)
bel[x + 1] = i, dfs(x + 1, tot);
bel[x + 1] = tot + 1, dfs(x + 1, tot + 1);
} int main(){
scanf("%d %d %d", &n, &l, &k);
for(int i = 1; i <= n; ++i)
scanf("%s", s[i] + 1);
if(k >= 7){
printf("%d", n * (n - 1) / 2);
return 0;
}
k = 8 - k;
for(int i = 1; i <= l; ++i){
has[i] = Ra();
}
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= l; ++j){
inc[i][s[i][j] - 'a' + 1] ^= has[j];
}
dfs(0, 0);
printf("%d\n", ans);
return 0;
}

2018.9.9 nowcoder 普及组第一场的更多相关文章

  1. [比赛|考试]nowcoder NOIPpj组第二场

    nowcoder NOIPpj组第二场 370pts/400pts(100,100,100,70) rank3 给自己的反思:前3题都A了,T4O(N^2)不会就是不会(没准是我懒得推了),DP了70 ...

  2. 纪中10日T1 2300. 【noip普及组第一题】模板题

    2300. [noip普及组第一题]模板题 (File IO): input:template.in output:template.out 时间限制: 1000 ms  空间限制: 262144 K ...

  3. 洛谷P1067 多项式输出 NOIP 2009 普及组 第一题

    洛谷P1067 多项式输出 NOIP 2009 普及组 第一题 题目描述 一元n次多项式可用如下的表达式表示: 输入输出格式 输入格式 输入共有 2 行 第一行 1 个整数,n,表示一元多项式的次数. ...

  4. noip2003复赛普及组第一题——乒乓球

    /*======================================================================= 题一.乒乓球(Table.pas) [问题背景]国际 ...

  5. 2018 计蒜之道-初赛 第一场 A-百度无人车

    百度一共制造了 nn 辆无人车,其中第 ii 辆车的重量为 a_i\ \mathrm{kg}ai​ kg. 由于车辆过重会增大轮胎的磨损程度,现在要给这 nn 辆车减轻重量.每将一辆车减轻 1\ \m ...

  6. hash(2018年CSUST省赛选拔赛第一场B题+hash+字典树)

    题目链接:http://csustacm.com:4803/problem/1006 题目: 思路:正如题目一样,本题是一个hash,比赛的时候用的字典树,但是不知道为什么一直RE(听学长说要动态开点 ...

  7. 牛客NOIP暑期七天营-提高组5+普及组5

    ————提高组———— 第一题:deco的abs 题目链接:https://ac.nowcoder.com/acm/contest/934/A 因为每个数都可以加任意次 d ,所以可以推出 0 < ...

  8. NOIP2008 普及组T1 ISBN号码 解题报告-S.B.S.

    题目描述 每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括9位数字.1位识别码和3位分隔符,其规定格式如“x-xxx-xxxxx-x”,其中符号“-”就是分隔符(键盘上的减号),最后一 ...

  9. NOIP2008普及组 题解 -SilverN

    T1 ISBN号码 题目描述 每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括9位数字.1位识别码和3位分隔符, 其规定格式如“x-xxx-xxxxx-x”,其中符号“-”就是分隔符( ...

随机推荐

  1. python之字符串split和rsplit的方法

    1.描述 split()方法通过指定分隔符对字符串进行切片,如果参数num有指定值,则分隔num+1个子字符串,默认分隔符为所有空字符,包括空格.换行(\n).制表符(\t)等 rstrip()方法通 ...

  2. 牛客网多校第4场 J Hash Function 【思维+并查集建边】

    题目链接:戳这里 学习博客:戳这里 题意: 有n个空位,给一个数x,如果x%n位数空的,就把x放上去,如果不是空的,就看(x+1)%n是不是空的. 现在给一个已经放过数的状态,求放数字的顺序.(要求字 ...

  3. zoj-3872 Beauty of Array (dp)

    ]Edward has an array A with N integers. He defines the beauty of an array as the summation of all di ...

  4. Leetcode(13)-罗马数字转整数

    罗马数字包含以下七种字符:I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即为两个并列 ...

  5. Linux的IO调度程序

    Linux的IO调度机制浅析 现代计算机体系中,磁盘的速度和CPU的速度差距太大了,如果简单的将系统的IO请求按照请求的顺序进行顺序处理的话,系统的IO开销将导致系统的效率十分的低下,因此就需要将IO ...

  6. auto scroll bottom in js

    auto scroll bottom in js autoScrollToBottom() { let box = document.querySelector(`[data-dom="ch ...

  7. taro demos & taro 组件库

    taro demos & taro 组件库 ui demo https://github.com/qit-team/taro-yanxuan https://github.com/fengch ...

  8. how to change sketch language to chinese

    how to change sketch language to Chinese https://www.sketch.com/support/troubleshooting/chinese-loca ...

  9. 10月上线的NGK global有怎样的发展前景?

    随着NGK global 10月份的上线时间将近,社区中也开始纷纷讨论.预测起NGK global上线后的表现,对此小编也有一些自己的理解,就此分享给大家. 在基于实体生态的赋能下,NGK globa ...

  10. [转]自动驾驶平台Apollo 2.5环境搭建

    原文地址:https://blog.csdn.net/jinzhuojun/article/details/80210180,转载主要方便随时查阅,如有版权要求,请及时联系. 我们知道,自动驾驶在学界 ...