水 A- Bulbs

#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)的更多相关文章

  1. Codeforces Round #338 (Div. 2) E. Hexagons 讨论讨论

    E. Hexagons 题目连接: http://codeforces.com/contest/615/problem/E Description Ayrat is looking for the p ...

  2. Codeforces Round #338 (Div. 2) D. Multipliers 数论

    D. Multipliers 题目连接: http://codeforces.com/contest/615/problem/D Description Ayrat has number n, rep ...

  3. 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 ...

  4. Codeforces Round #338 (Div. 2) B. Longtail Hedgehog dp

    B. Longtail Hedgehog 题目连接: http://www.codeforces.com/contest/615/problem/B Description This Christma ...

  5. Codeforces Round #338 (Div. 2) A. Bulbs 水题

    A. Bulbs 题目连接: http://www.codeforces.com/contest/615/problem/A Description Vasya wants to turn on Ch ...

  6. Codeforces Round #338 (Div. 2) D 数学

    D. Multipliers time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  7. Codeforces Round #338 (Div. 2) B dp

    B. Longtail Hedgehog time limit per test 3 seconds memory limit per test 256 megabytes input standar ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 怎么查询电脑ip地址

    方法一:本地连接查看法 方法二:命令行法 摘自:http://jingyan.baidu.com/article/870c6fc3d509a1b03fe4be06.html

  2. ASINetworkQueues(经典2)

    ASINetworkQueues, 它的delegate提供更为丰富的功能 提 供的更多的回调方法如下: a,requestDidStartSelector,请求发起时会调此方法,你可以在此方法中跟据 ...

  3. ios手势

    iOS 手势操作:拖动.捏合.旋转.点按.长按.轻扫.自定义 大 中 小   1.UIGestureRecognizer 介绍 手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性. i ...

  4. Redis事件管理(三)

    Redis的事件管理和定时器的管理都是自己来实现的,Redis的事件管理分为两部分,一部分是封装了系统的异步事件API,还有一部分是在这基础上封装了一个通用的事件管理器,根据具体的系统来决定具体使用哪 ...

  5. 常用shell命令操作

    1.找出系统中所有的*.c 和*.h 文件 (-o 或者) $find / -name "*.cpp" -o -name "*.h" 2.设定 eth0 的 I ...

  6. KMP算法学习

    kmp算法完成的任务是:给定两个字符串O和f,长度分别为n和m,判断f是否在O中出现,如果出现则返回出现的位置.常规方法是遍历a的每一个位置,然后从该位置开始和b进行匹配,但是这种方法的复杂度是O(n ...

  7. 四、优化及调试--网站优化--Yahoo军规上

    什么是Yahoo军规?即如何提高网站速度的知识. 具体如下: 1.尽量减少HTTP请求个数——须权衡 什么是http请求:从客户端到服务器端的请求消息.包括消息首行中,对资源的请求方法,资源的标识符及 ...

  8. CLR via C#(17)--接口

    CLR不允许继承多个基类,但是可以继承多个接口.凡是能使用具名接口类型的实例的地方,都能使用实现了接口的一个类型的实例. 接口是对一组方法签名进行了统一命名,但不提供任何实现,而具体类则必须为继承的全 ...

  9. Delphi之DLL知识学习4---创建DLL

    下面是在Delphi中创建一个DLL的全过程,你将看到怎样创建一个接口单元,使之可以被其他的应用程序访问.并且将学会怎么把Delphi的窗体加入DLL中. 一.数美分:一个简单的DLL 下面是包含一个 ...

  10. react.js基本操练

    慢慢了解其它的JS前端框架... var data = [{ "when": "2 minutes ago", "who": "J ...