Codeforces Round #338 (Div. 2)
#include <bits/stdc++.h>
using namespace std; typedef long long ll;
const int N = 1e5 + 5;
const int INF = 0x3f3f3f3f;
bool vis[110]; int main(void) {
memset (vis, false, sizeof (vis));
int n, m; scanf ("%d%d", &n, &m);
for (int i=1; i<=n; ++i) {
int x; scanf ("%d", &x);
for (int j=1; j<=x; ++j) {
int y; scanf ("%d", &y);
if (!vis[y]) vis[y] = true;
}
}
bool flag = true;
for (int i=1; i<=m; ++i) {
if (!vis[i]) {
flag = false; break;
}
}
if (flag) puts ("YES");
else puts ("NO"); return 0;
}
DFS+DP B - Longtail Hedgehog
题意:找一条递增的链,最后一个点的度数乘链的长度最大。
分析:DFS写搓了,记忆化搜索dp能优化复杂度,也可以直接两个for贪心解决。本以为不会有极端的数据的。。。
#include <bits/stdc++.h>
using namespace std; typedef long long ll;
const int N = 1e5 + 5;
const int M = 2e5 + 5;
const int INF = 0x3f3f3f3f;
vector<int> G[N];
int dp[N];
bool vis[N];
int n, m; int DFS(int u) {
if (dp[u]) return dp[u];
dp[u] = 1;
for (int i=0; i<G[u].size (); ++i) {
int v = G[u][i];
if (v < u) dp[u] = max (dp[u], DFS (v) + 1);
}
return dp[u];
} ll run(void) {
fill (dp+1, dp+1+n, 0);
ll ret = 0;
for (int i=1; i<=n; ++i) {
ret = max (ret, 1ll * DFS (i) * (int) G[i].size ());
}
return ret;
} int main(void) {
scanf ("%d%d", &n, &m);
for (int u, v, i=1; i<=m; ++i) {
scanf ("%d%d", &u, &v);
G[u].push_back (v);
G[v].push_back (u);
}
printf ("%I64d\n", run ()); return 0;
}
组合数学 D - Multipliers
题意:给定m个质因子p[],有p[1]*p[2]*...*p[m] == n,问n所有因子的乘积 % 1e9 + 7。
分析:m个质因子可能有重复的,n = p[i] ^ k[i],k[i]表示p[i]在乘积里贡献了几次,k[i] = (num[i] * (num[i] + 1)) / 2 * (num[j] + 1) (i != j);也就是p[j]可能出现有0,1,2...num[j]。由费马小定理:a ^ x = a ^ (x % (p - 1)) (mod p),k[i]能变形成:(num[i] / 2) * (num[j] + 1),但是p - 1不是质数,不能用求2的逆元,可以用mod2 = 2 * (1e9 + 7) ???
#include <bits/stdc++.h>
using namespace std; typedef long long ll;
const int N = 2e5 + 5;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int MOD2 = 2ll * (MOD - 1);
int m;
int p[N], num[N]; int pow_mod(int x, ll n, int mod) {
int ret = 1;
while (n) {
if (n & 1) ret = 1ll * ret * x % mod;
x = 1ll * x * x % mod; n >>= 1;
}
return ret;
} int main(void) {
scanf ("%d", &m);
for (int i=1; i<=m; ++i) {
scanf ("%d", &p[i]);
num[p[i]]++;
}
sort (p+1, p+1+m);
int n = unique (p+1, p+1+m) - p - 1;
int tot = 1;
for (int i=1; i<=n; ++i) {
tot = 1ll * tot * (num[p[i]] + 1) % MOD2;
}
int ans = 1;
for (int i=1; i<=n; ++i) {
ans = 1ll * ans * pow_mod (p[i], 1ll * num[p[i]] * tot / 2, MOD) % MOD;
}
printf ("%d\n", ans); return 0;
}
Trie || KMP C - Running Track
题意:给s和t字符串,问最少的分割t的子串来自s,输出起点和终点
分析:贪心的想法,每次求最长的子串,可以用字典树来查找也可以直接用KMP
Trie
#include <bits/stdc++.h>
using namespace std; typedef pair<int, int> P;
typedef long long ll;
const int N = 2100 + 5;
const int INF = 0x3f3f3f3f;
struct Trie {
int ch[N*N][26], sz;
P pr[N*N];
int idx(char c) {
return c - 'a';
}
void clear(void) {
sz = 1; memset (ch[0], 0, sizeof (ch[0]));
}
void insert(char *str, int from, int dr) {
int u = 0;
for (int c, i=from; i>=0&&str[i]; i+=dr) {
c = idx (str[i]);
if (!ch[u][c]) {
memset (ch[sz], 0, sizeof (ch[sz]));
ch[u][c] = sz++;
}
u = ch[u][c];
pr[u] = make_pair (from, i);
}
}
void query(char *str, vector<P> &vec) {
int u = 0;
for (int c, i=0; str[i]; ++i) {
c = idx (str[i]);
if (!ch[u][c]) {
if (u == 0) {
vec.clear (); return ;
}
vec.push_back (pr[u]); u = 0; i--;
}
else u = ch[u][c];
}
vec.push_back (pr[u]);
}
}trie;
char s[N], t[N]; int main(void) {
scanf ("%s%s", &s, &t);
trie.clear ();
for (int i=0; s[i]; ++i) {
trie.insert (s, i, 1);
trie.insert (s, i, -1);
}
vector<P> ans;
trie.query (t, ans);
if (ans.size () == 0) {
puts ("-1"); return 0;
}
printf ("%d\n", (int) ans.size ());
for (int i=0; i<ans.size (); ++i) {
printf ("%d %d\n", ans[i].first + 1, ans[i].second + 1);
} return 0;
}
KMP
#include <bits/stdc++.h>
using namespace std; typedef pair<int, int> P;
typedef long long ll;
const int N = 2100 + 5;
const int INF = 0x3f3f3f3f;
char s[N], rs[N], t[N];
int fail[N]; void get_fail(char *P, int lenp) {
int i = 0, j = -1; fail[0] = -1;
while (i < lenp) {
if (j == -1 || P[j] == P[i]) {
i++; j++; fail[i] = j;
}
else j = fail[j];
}
} P KMP(char *T, char *P) {
int lent = strlen (T), lenp = strlen (P);
get_fail (P, lenp);
int i = 0, j = 0, max_len = 0, beg = -1;
while (i < lent) {
while (j != -1 && T[i] != P[j]) j = fail[j];
i++; j++;
if (j > max_len) {
max_len = j; beg = i - j;
}
if (j == lenp) break;
}
return make_pair (beg, beg + max_len - 1);
} bool add_answer(P p1, P p2, vector<P> &ans, int len, int &i) {
P ap = p1, bp = make_pair (len - p2.first - 1, len - p2.second - 1);
int la = p1.second - p1.first, lb = p2.second - p2.first;
if (p1.first == -1) {
if (p2.first == -1) return false;
ans.push_back (bp); i += lb;
}
else if (p2.first == -1) {
if (p1.first == -1) return false;
ans.push_back (ap); i += la;
}
else {
if (la > lb) {
ans.push_back (ap); i += la;
}
else {
ans.push_back (bp); i += lb;
}
}
return true;
} int main(void) {
scanf ("%s%s", &s, &t);
int lens = strlen (s), lent = strlen (t);
for (int i=0; i<lens; ++i) rs[i] = s[lens-i-1];
vector<P> ans;
for (int i=0; i<lent; ++i) {
P pr1 = KMP (s, t + i);
P pr2 = KMP (rs, t + i);
if (!add_answer (pr1, pr2, ans, lens, i)) {
puts ("-1"); return 0;
}
}
printf ("%d\n", (int) ans.size ());
for (int i=0; i<ans.size (); ++i) {
printf ("%d %d\n", ans[i].first + 1, ans[i].second + 1);
} return 0;
}
Codeforces Round #338 (Div. 2)的更多相关文章
- Codeforces Round #338 (Div. 2) E. Hexagons 讨论讨论
E. Hexagons 题目连接: http://codeforces.com/contest/615/problem/E Description Ayrat is looking for the p ...
- Codeforces Round #338 (Div. 2) D. Multipliers 数论
D. Multipliers 题目连接: http://codeforces.com/contest/615/problem/D Description Ayrat has number n, rep ...
- Codeforces Round #338 (Div. 2) C. Running Track dp
C. Running Track 题目连接: http://www.codeforces.com/contest/615/problem/C Description A boy named Ayrat ...
- Codeforces Round #338 (Div. 2) B. Longtail Hedgehog dp
B. Longtail Hedgehog 题目连接: http://www.codeforces.com/contest/615/problem/B Description This Christma ...
- Codeforces Round #338 (Div. 2) A. Bulbs 水题
A. Bulbs 题目连接: http://www.codeforces.com/contest/615/problem/A Description Vasya wants to turn on Ch ...
- Codeforces Round #338 (Div. 2) D 数学
D. Multipliers time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- Codeforces Round #338 (Div. 2) B dp
B. Longtail Hedgehog time limit per test 3 seconds memory limit per test 256 megabytes input standar ...
- Codeforces Round #338 (Div. 2) B. Longtail Hedgehog 记忆化搜索/树DP
B. Longtail Hedgehog This Christmas Santa gave Masha a magic picture and a pencil. The picture con ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
随机推荐
- PHP安全编程:过滤用户输入
如果你能正确可靠地识别和过滤输入,你的工作就基本完成了.最后一步是使用一个命名约定或其它可以帮助你正确和可靠地区分已过滤和被污染数据的方 法.我推荐一个比较简单的命名约定,因为它可以同时用在面向过程和 ...
- Myeclipse for Mac快捷键
myeclipse for mac 的快捷键汇总 快键键 作用 备注 Command+1 快速修复(自动导包等) 比如与Syso配合,与main配合可快速构造方法签名 Alt+/ 自动补全 Comma ...
- nfs server的配置 Starting NFS daemon: [FAILED]
总结了一下是nfs server的制作过程:nfs(Network File System)其实就是说,这个机器的硬盘不够了,我要把文件放到别的服务器上去,服务器端的配置如下:首先(1)确保你的机器上 ...
- 关于HTML5在动画制作工具Animatron的一些问题
Animatron是国外一款在线HTML5动画制作工具,网址:www.animatron.com 当然,想使用的话,是需要FQ的. 用animatron制作好的动画是可以下载为代码和GIF的,这时候付 ...
- 清除Windows系统桌面快捷方式小箭头
清除Windows桌面快捷方式小箭头,需要重启,且不会导致软件无法锁定到任务栏.新建.reg的注册表文件,命名随意,内容如下: Windows Registry Editor Version 5.00 ...
- NYOJ题目114某种序列
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAscAAAHuCAIAAAD83zYaAAAgAElEQVR4nO3dP1LjygIv4LcJ5yyE2A
- python中获取当前日期在当月是第几天
- 登录到mysql查看binlog日志
查看当前第一个binlog文件的内容 show binlog events; 查看指定binlog文件内容 show binlog events in 'mysql-bin.000002'; 查看当前 ...
- Jquery.Datatables td宽度太长的情况下,自动换行
在 td 里面 加上 style="word-wrap:break-word;" 自动换行就好了,如果不想换行,可以将超出内容设为隐藏, overflow:hidden; whit ...
- 在python3.5下安装scrapy包
此前scrapy只支持python2.x 但是最新的1.1.0rc1已结开始支持py3了 如果电脑上安装了scrapy的依赖包,诸如lxml.OpenSSL 1.你直接下载Scrapy-1.1.0rc ...