NorthAmericanSoutheastRegional2019部分题解

  1. 1. A - Carryless Square Root
    1. 1.0.0.1. 题目大意
    2. 1.0.0.2. 代码
  • 2. D - Swap Free
    1. 2.0.0.1. 题目大意
    2. 2.0.0.2. 代码
  • 3. H - Levenshtein Distance
    1. 3.0.0.1. 题目大意
    2. 3.0.0.2. 代码

  • A - Carryless Square Root

    题目大意

    给定一个数n,让你找出最小的a使得 a*a 在无进位乘法下等于n

    代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    //dfs 但是有一些细节问题需要注意
    #include <bits/stdc++.h>

    using namespace std;
    const int N = 2e2 + 10;
    const int INF = 0x3f3f3f3f;
    const long long LINF = 0x3f3f3f3f3f3f3f3f;
    const unsigned long long P = 1e9 + 7;
    const double eps = 1e-6;
    string s;
    int a[N],b[N],c[N];
    int Max = 0;
    //判断到当前位数下是否符合条件
    bool check(int pos){
    memset(c,0,sizeof c);
    for(int i = 0;i<=pos;i++){
    for(int j = 0;j<=pos;j++){
    if(pos!=Max-1&&i+j>pos) break;
    c[i+j] += (b[i]*b[j])%10;
    c[i+j] %= 10;
    }
    }
    for(int i = 0;i<=(pos==Max-1?Max*2-2:pos);i++){
    if(c[i]!=a[i]) return false;
    }
    return true;
    }
    //dfs找
    void dfs(int pos){
    if(pos==Max){
    if(!check(pos-1)) return;
    for(int j = 0;j<pos;j++) cout<<b[j];
    exit(0);
    }
    for(int i = 0;i<=9;i++){
    b[pos] = i;
    if(check(pos)){
    dfs(pos+1);
    }
    }
    }
    int main() {
    cin>>s;
    int n = s.size();
    if(n%2==0) {
    cout<<"-1\n";
    return 0;
    }
    for(int i = 0;i<s.size();i++){
    a[i] = s[i]-'0';
    }
    Max = (n+1)/2;
    dfs(0);
    cout<<"-1\n";
    return 0;
    }

    D - Swap Free

    题目大意

    给定一个字符串集合,集合内单词都互为排列,即单词无重复、使用字母完全相同。问找出一个最大子集,使得该集合内单词互相之间无法通过交换一对字母得到。

    代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    //找到两两可以放入同一个集合的单词 跑一遍二分图
    #include <bits/stdc++.h>

    using namespace std;
    const int N = 505;
    char str[N][30];
    int match[N];
    bool vis[N];
    vector<int> g[N];

    bool check(char s[], char str[], int n) {
    int num = 0;
    for (int i = 0; i < n; i++)
    if (s[i] != str[i]) num++;
    return num == 2;
    }

    bool dfs(int u) {
    for (int i = 0, v; i < g[u].size(); i++) {
    if (!vis[v = g[u][i]]) {
    vis[v] = true;
    if (!match[v] || dfs(match[v])) {
    match[v] = u;
    return true;
    }
    }
    }
    return false;
    }

    int Get(int n) {
    memset(match, 0, sizeof match);
    int num = 0;
    for (int i = 1; i <= n; i++) {
    memset(vis, 0, sizeof vis);
    if (dfs(i)) num++;
    }
    return num / 2;
    }

    int main() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
    scanf("%s", str[i]);
    }
    int len = strlen(str[1]);
    for (int i = 1; i <= n; i++)
    for (int j = 1; j < i; j++)
    if (check(str[i], str[j], len)) {
    g[i].push_back(j);
    g[j].push_back(i);
    }
    int res = n - Get(n);
    printf("%d\n", res);
    return 0;
    }

    H - Levenshtein Distance

    题目大意

    给定使用字母和一个单词,让你找出所有使用该字母表并且仅对该单词操作一次的字符串,操作可以是添加、删除或替换一个字母。

    代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    //签到题 暴力跑一遍
    #include <bits/stdc++.h>

    using namespace std;
    set<string> se;
    string s, str;

    int main() {
    ios::sync_with_stdio(false);
    cin >> s >> str;
    for (int i = 0; i < str.size(); i++) {
    string s2 = str;
    auto it = s2.begin() + i;
    s2.erase(it);
    se.insert(s2);
    }
    for (auto c : s) {
    for (int i = 0; i < str.size(); i++) {
    char cc = str[i];
    if (c == cc) continue;
    str[i] = c;
    se.insert(str);
    str[i] = cc;
    se.insert(str.substr(0, i) + c +
    str.substr(i, (int)str.size() - i));
    }
    se.insert(str + c);
    }
    for (auto ss : se) {
    cout << ss << '\n';
    }
    return 0;
    }