[hihocoder] [Offer收割]编程练习赛43

版本号排序

不知道什么傻逼原因,就是过不了

#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<functional>
#include<math.h>
//#include<bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII;
void makedata() {
    freopen("input.txt", "w", stdout);
    cout << 200000 << endl;

    for(int i = 0; i < 200000; i++) cout << 1000000000 << ‘ ‘;

    fclose(stdout);
}

VI a[200];
bool ok(int x, int y) {
    int lx = a[x].size(), ly = a[y].size();
    int l = min(lx, ly);

    for(int i = 0; i < l; i++) {
        if(a[x][i] < a[y][i]) return true;

        if(a[x][i] > a[y][i]) return false;
    }

    if(lx <= ly) return true;
    else return false;
}
int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    //makedata();
    std::ios::sync_with_stdio(0), cin.tie(0);
    int n;
    cin >> n;

    for(int i = 0; i < n; i++) {
        int tmp;
        cin >> tmp;
        a[i].push_back(tmp);

        while(getchar() == ‘.‘) {
            cin >> tmp;
            a[i].push_back(tmp);
        }
    }

    for(int i = 0; i < n; i++) {
        for(int j = i + 1; j < n; j++) {
            if(!ok(i, j)) swap(a[i], a[j]);
        }
    }

    for(int i = 0; i < n; i++) {
        cout << a[i][0];

        for(int j = 1; j < a[i].size(); j++) cout << ‘.‘ << a[i][j];

        cout << endl;
    }

    return 0;
}

自底向上遍历二叉树

叶节点从左到右的顺序就是它们在树的从上到下的遍历中的顺序,按遍历中的顺序不断向上找

#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<functional>
#include<math.h>
//#include<bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII;
void makedata() {
    freopen("input.txt", "w", stdout);
    cout << 200000 << endl;

    for(int i = 0; i < 200000; i++) cout << 1000000000 << ‘ ‘;

    fclose(stdout);
}

VI ch[110000];
bool f[110000];
int father[110000];
void traverse(int x) {
    while(f[x] == false && x) {
        cout << x << endl;
        f[x] = true;
        x = father[x];
    }
}
void dfs(int x) {
    if(ch[x].size() == 0) traverse(x);
    else for(int i = 0; i < ch[x].size(); i++) dfs(ch[x][i]);
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    //makedata();
    std::ios::sync_with_stdio(0), cin.tie(0);
    int n;
    cin >> n;
    memset(father, 0, sizeof(father));

    for(int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        father[v] = u;
        ch[u].push_back(v);
    }

    for(int i = 1; i <= n; i++) {
        if(ch[i].size() != 2) continue;

        if(ch[i][0] > ch[i][1]) {
            int tmp = ch[i][0];
            ch[i][0] = ch[i][1];
            ch[i][1] = tmp;
        }
    }

    int root = 1;

    while(father[root]) root = father[root];

    memset(f, false, sizeof(f));
    dfs(root);
    return 0;
}

hiho字符串2

根据长度dfs,注意一开始是第1代而不是第0代。

#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<functional>
#include<math.h>
//#include<bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII;
void makedata() {
    freopen("input.txt", "w", stdout);
    cout << 200000 << endl;

    for(int i = 0; i < 200000; i++) cout << 1000000000 << ‘ ‘;

    fclose(stdout);
}

lint L[256][200];
const lint INF = (1LL << 40);

char dfs(string s, int n, lint k) {
    char ch = s[0];
    int len = s.size();
    string ss;

    if(n == 0) {
        if(k > len) return 0;
        else return s[k - 1];
    }

    if(L[ch][n] >= k) {
        if(ch == ‘h‘) ss = "hio";

        if(ch == ‘i‘) ss = "hi";

        if(ch == ‘o‘) ss = "ho";

        return dfs(ss, n - 1, k);
    } else {
        k -= L[ch][n];
        ss = s.substr(1, len - 1);
        return dfs(ss, n, k);
    }
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    //makedata();
    std::ios::sync_with_stdio(0), cin.tie(0);
    L[‘h‘][0] = L[‘i‘][0] = L[‘o‘][0] = 1;

    for(int i = 1; i <= 150; i++) {
        L[‘h‘][i] = L[‘h‘][i - 1] + L[‘i‘][i - 1] + L[‘o‘][i - 1];
        L[‘i‘][i] = L[‘h‘][i - 1] + L[‘i‘][i - 1];
        L[‘o‘][i] = L[‘h‘][i - 1] + L[‘o‘][i - 1];

        if(L[‘h‘][i] > INF) L[‘h‘][i] = INF;

        if(L[‘i‘][i] > INF) L[‘i‘][i] = INF;

        if(L[‘o‘][i] > INF) L[‘o‘][i] = INF;
    }

    int t, n;
    lint k;
    cin >> t;
    string s = "hiho";

    while(t--) {
        cin >> n >> k;
        cout << dfs(s, n - 1, k) << endl;;
    }

    return 0;
}

最长多数子串

一开始想的是二分+hash,没过。别人用的SAM,想破了脑袋也想不清楚。以前看过SAM,当时看完就觉得:“好叼啊,可是有什么用啊?”,怎么也想不通在哪里能用上,这里别人用了,怎么也想不通到底怎么过的。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<memory>
#include<cmath>
#define maxn 2000003
using namespace std;
int n, m, len, ans, Max, now;
char s[maxn], cap[maxn];
struct SAM {
    int ch[maxn][26], fa[maxn], maxlen[maxn], Last, sz;
    int root, nxt[maxn], size[maxn];
    void init() {
        sz = 0;
        root = ++sz;
        memset(size, 0, sizeof(size));
        memset(ch[1], 0, sizeof(ch[1]));
        memset(nxt, 0, sizeof(nxt));
    }
    void add(int x) {
        int np = ++sz, p = Last;
        Last = np;
        memset(ch[np], 0, sizeof(ch[np]));
        maxlen[np] = maxlen[p] + 1;
        while (p && !ch[p][x]) ch[p][x] = np, p = fa[p];
        if (!p) fa[np] = 1;
        else {
            int q = ch[p][x];
            if (maxlen[p] + 1 == maxlen[q]) fa[np] = q;
            else {
                int nq = ++sz;
                memcpy(ch[nq], ch[q], sizeof(ch[q]));
                size[nq] = size[q];
                nxt[nq] = nxt[q];
                maxlen[nq] = maxlen[p] + 1;
                fa[nq] = fa[q];
                fa[q] = fa[np] = nq;
                while (p && ch[p][x] == q) ch[p][x] = nq, p = fa[p];
            }
        }
        for (; np; np = fa[np])
            if (nxt[np] != now) {
                size[np]++;
                nxt[np] = now;
            } else break;
    }
};
SAM Sam;
int main() {
    while (~scanf("%d%d", &n, &m) && n) {
        Sam.init();
        for (int i = 1; i <= n; i++) {
            scanf("%s", s + 1);
            Sam.Last = Sam.root;
            len = strlen(s + 1);
            now = i;
            for (int j = 1; j <= len; j++) Sam.add(s[j] - ‘a‘);
        }
        Max = 0;
        ans = 0;
        for (int i = 1; i <= Sam.sz; i++)
            if (Sam.size[i] >= m && Sam.maxlen[i] > ans) {
                Max = i;
                ans = Sam.maxlen[i];
            }
        printf("%d\n", ans);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/dramstadt/p/8251562.html

时间: 2024-10-26 01:50:22

[hihocoder] [Offer收割]编程练习赛43的相关文章

hihocoder - [Offer收割]编程练习赛17

hihocoder - [Offer收割]编程练习赛17 题目1 : F1 Score 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和他的小伙伴们一起写了很多代码.时间一久有些代码究竟是不是自己写的,小Hi也分辨不出来了. 于是他实现了一个分类算法,希望用机器学习实现自动分类. 为了评价这个分类算法的优劣,他选出了N份有标记的代码作测试集,并决定用F1 Score作为评价标准. 给出N份代码的实际作者是不是小Hi以及分类算法预测的结果,请你计算F1 Sco

hihocoder [Offer收割]编程练习赛18 C 最美和弦(dp)

题目链接:http://hihocoder.com/problemset/problem/1532 题解:一道基础的dp,设dp[i][j][k][l]表示处理到第几个数,当前是哪个和弦错了几次初始x值是多少.这里还要再辅助一个val[k]表示处理到当前情况只错了k次的最小值是多少因为改变的不止是和弦还有初始值,可以看一下代码理解一下. #include <iostream> #include <cstring> #include <cstdio> #include &

hihocoder offer收割编程练习赛13 D 骑士游历

思路: 矩阵快速幂. 实现: 1 #include <iostream> 2 #include <cstdio> 3 #include <vector> 4 using namespace std; 5 6 typedef long long ll; 7 typedef vector<ll> vec; 8 typedef vector<vec> mat; 9 10 const ll mod = 1e9 + 7; 11 12 ll n, x, y;

hihocoder offer收割编程练习赛12 C 矩形分割

思路: 模拟,深搜. 实现: 1 #include <iostream> 2 #include <cstdio> 3 #include <string> 4 using namespace std; 5 6 const int dx[4] = { 0, 1, 0, -1 }; 7 const int dy[4] = { -1, 0, 1, 0 }; 8 9 int n, m, cnt = 0; 10 int a[305][305]; 11 bool vis[305][3

hihocoder offer收割编程练习赛12 A 歌德巴赫猜想

思路: 枚举. 实现: 1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 6 const int MAX_N = 1000005; 7 8 int prime[MAX_N]; 9 bool is_prime[MAX_N + 1]; 10 11 int init(int n) 12 { 13 int p = 0; 14 for (int i =

hihocoder offer收割编程练习赛11 A hiho字符串

思路: 我用的尺取. 注意题目描述为恰好2个'h',1个'i',1个'o'. 实现: 1 #include <iostream> 2 #include <cstdio> 3 #include <string> 4 #include <algorithm> 5 using namespace std; 6 7 const int INF = 0x3f3f3f3f; 8 9 int ch[100005], ci[100005], co[100005]; 10 1

hihocoder offer收割编程练习赛11 B 物品价值

思路: 状态压缩 + dp. 实现: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 7 const int INF = 0x3f3f3f3f; 8 9 int t, n, m, s; 10 int V[1005], S[1005], dp[1005][(1 << 11) +

hihocoder offer收割编程练习赛11 C 岛屿3

思路: 并查集的应用. 实现: 1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 5 bool a[1005][1005]; 6 int n, x, y; 7 int par[1000005]; 8 int ran[1000005]; 9 int dx[4] = { 1, 0, -1, 0 }; 10 int dy[4] = { 0, 1, 0, -1 }; 11 12 void init(int

hihocoder offer收割编程练习赛8 B

思路: 模拟,dfs. 注意题目中的trick,输出一块的时候不要把其他块也输出了. 实现: 1 #include <cstring> 2 #include <iostream> 3 #include <cstdio> 4 #include <algorithm> 5 using namespace std; 6 7 char a[505][505]; 8 int n, m, minx, miny, maxx, maxy; 9 bool vis[505][5