水 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. pmap

    .[root@localhost security]# pmap -d : -bash Address Kbytes Mode Offset Device Mapping r-x-- : bash b ...

  2. Android错误:Re-installation failed due to different application signatures

    Re-installation failed due to different application signatures (2013-04-20 14:27:32) 转载▼ 标签: 解决方法 问题 ...

  3. Android-----overridePendingTransition的使用

    1 Activity的切换动画指的是从一个activity跳转到另外一个activity时的动画. 它包括两个部分:一部分是第一个activity退出时的动画:另外一部分时第二个activity进入时 ...

  4. 解决IIS7、IIS7.5中时间格式显示的问题

    今天在用IIS7的时候发现一个关于时间格式的问题,当我在ASP中使用now()时间函数的时候,日期是以"/"来分隔,而不是以"-"来分隔的,使得我在运行程序的时 ...

  5. html 中几次方,平方米,立方米,下标,上标,删除线等的表示方法

    html 中几次方,平方米,立方米,上标,下标,删除线等的表示方法 上标下标删除线 小号字  M2 54 X24+Y1<3=100 NN <sup>上标</sup> &l ...

  6. 【JAVA之泛型】

    一.引例. 1.引例. 假设现在有一个ArrayList的容器,如果不使用泛型约束,则可以向容器中加入各种类型的对象,但是如果取出来的时候只是用一种类型的转换则肯定会抛出ClassCastExcept ...

  7. python中多线程与非线程的执行性能对比

    此对比说明了一件事: 如果是IO型应用,多线程有优势, 如果是CPU计算型应用,多线程没必要,还有实现锁呢. #!/usr/bin/env python # -*- coding: utf-8 -*- ...

  8. 配置ogg异构oracle-mysql(1)基础环境配置

    一.环境描述: 192.168.0.164 ( Oracle ) —> 192.168.0.165 (Mysql ) 版本: 操作系统:redhat5.8 Oracle:  11.2.0.3 M ...

  9. zzy:java采用的是16位的Unicode字符集作为编码方式------理解

    java语言使用16位的Unicode字符集作为编码方式,是疯狂Java中的原话. 1,编码方式只是针对字符类型的(不包括字符串类,数值类型int等,这些只是在解释[执行]的时候放到Jvm的不同内存块 ...

  10. linux应用程序地址布局,王明学learn

    linux应用程序地址布局 在学习Linux应用程序开发时,经常会遇到如下概念:代码段.数据段.BSS段(Block Started by Symbol,又名:未初始化数据段).堆(heap)和栈(s ...