ARC097

C K-th Substring

Problem Statement
You are given a string s. Among the different substrings of s, print the K-th lexicographically smallest one.
A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = ababc, a, bab and ababc are substrings of s, while ac, z and an empty string are not. Also, we say that substrings are different when they are different as strings.

Let X=x1x2…xn and Y=y1y2…ym be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or xj>yj where j is the smallest integer such that xj≠yj.

Constraints
1 ≤ |s| ≤ 5000
s consists of lowercase English letters.
1 ≤ K ≤ 5
s has at least K different substrings.

Partial Score
200 points will be awarded as a partial score for passing the test set satisfying |s| ≤ 50.

Input
Input is given from Standard Input in the following format:
s
K

Output
Print the K-th lexicographically smallest substring of K.

Sample Input 1
aba
4

Sample Output 1
b
s has five substrings: a, b, ab, ba and aba. Among them, we should print the fourth smallest one, b. Note that we do not count a twice.

Sample Input 2
atcoderandatcodeer
5

Sample Output 2
andat

Sample Input 3
z
1

Sample Output 3
z

题意

找出所有子串中字典序第K小的。

题解

注意到K最大为5,所有枚举长度小于5的所有子串排序或最大堆平衡树等方法计算第K小即可,注意去重。复杂度O(nklog(k))。

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
    string str;
    int k;
    cin >> str >> k;
    set<string> s;
    int sz = str.size();
    for (int i = 0; i < sz; i++) {
        string temp;
        for (int j = i; j < min(sz, i+k); j++) {
            temp += str[j];
            s.insert(temp);
        }
    }
    auto it = s.begin();
    //cout << s.size() << "\n";
    for (int i = 1; i < k; i++) {
        //cout << *it << "\n";
        it++;
    }
    cout <<*it << "\n";
    return 0;
}

D Equals

We have a permutation of the integers from 1 through N, p1, p2, .., pN. We also have M pairs of two integers between 1 and N (inclusive), represented as (x1,y1), (x2,y2), .., (xM,yM). AtCoDeer the deer is going to perform the following operation on p as many times as desired so that the number of i (1 ≤ i ≤ N) such that pi=i is maximized:
Choose j such that 1 ≤ j ≤ M, and swap pxj and pyj.
Find the maximum possible number of i such that pi=i after operations.

Constraints
2 ≤ N ≤ 105
1 ≤ M ≤ 105
p is a permutation of integers from 1 through N.
1 ≤ xj,yj ≤ N
xj ≠ yj
If i ≠ j, {xi,yi} ≠ {xj,yj}.
All values in input are integers.

Input
Input is given from Standard Input in the following format:
N M
p1 p2 .. pN
x1 y1
x2 y2
:
xM yM

Output
Print the maximum possible number of i such that pi=i after operations.

Sample Input 1
5 2
5 3 1 4 2
1 3
5 4

Sample Output 1
2
If we perform the operation by choosing j=1, p becomes 1 3 5 4 2, which is optimal, so the answer is 2.

Sample Input 2
3 2
3 2 1
1 2
2 3

Sample Output 2
3
If we perform the operation by, for example, choosing j=1, j=2, j=1 in this order, p becomes 1 2 3, which is obviously optimal. Note that we may choose the same j any number of times.

Sample Input 3
10 8
5 3 6 8 7 10 9 1 2 4
3 1
4 1
5 9
2 5
6 5
3 5
8 9
7 9

Sample Output 3
8

Sample Input 4
Copy
5 1
1 2 3 4 5
1 5

Sample Output 4
5
We do not have to perform the operation.

题意

给了一个数组,一堆pair,可随意交换任意次pair对应的下标的数,求数组里面下标等于元素值的最多对数。

题解

随便猜了个结论,可交换的下标组成多个联通块,每个联通块内部可以任意的permutation都可以通过有限次交换得到,也就是说可以转化为计算联通块内部index等于元素值的数量,通过dfs或者并查集很容易就可以求解。

证明可通过观察联通块对应的生成树,从叶子节点往上一一生成对应的permutation即可。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5;
int pa[maxn];
int f(int x) {
    return x == pa[x] ? x : (pa[x] = f(pa[x]));
}
void un(int x, int y) {
    int px = f(x);
    int py = f(y);
    if (px != py) {
        pa[px] = py;
    }
}
int main() {
    int n, m;
    cin >> n >> m;
    vector<int> nums(n+1);
    for (int i = 1; i <= n; i++) {
        cin >> nums[i];
    }
    for (int i = 0; i < maxn; i++) {
        pa[i] = i;
    }
    for (int i = 0; i < m; i++) {
        int x, y;
        cin >> x >> y;
        un(x, y);
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        if (f(nums[i]) == f(i)) {
            ans++;
        }
    }
    cout << ans << "\n";
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/hxidkd/p/9034236.html
arc