• 洛谷训练新手村之“简单字符串”题解


    P1055 ISBN号码

    题目链接:https://www.luogu.com.cn/problem/P1055
    题目大意:根据ISBN码的前9位判断最后一位是否正确。
    解题思路:模拟一家计算过程然后和最后一个数对照一下即可。
    实现代码如下:

    #include <bits/stdc++.h>
    using namespace std;
    #define MOD 11
    char s[20], a;
    int main() {
        cin >> s;
        for (int i = 0, j = 1; i < 11; i ++) {
            if (!isdigit(s[i])) continue;
            a = (a + (s[i] - '0') * (j ++)) % MOD;
        }
        if (a == 10 && s[12] == 'X' || a < 10 && s[12] == '0'+a)
            puts("Right");
        else {
            if (a == 10) s[12] = 'X';
            else s[12] = '0' + a;
            puts(s);
        }
        return 0;
    }
    

    P1200 [USACO1.1]你的飞碟在这儿Your Ride Is Here

    题目链接:https://www.luogu.com.cn/problem/P1200
    题目大意:计算两个字符串按照题目描述的转换规则是不是一样的。
    解题思路:编写一个函数用于获取字符串的Hash值,判断两个字符串的Hash值是否相等。
    实现代码如下:

    #include <bits/stdc++.h>
    using namespace std;
    int trans(char s[]) {
        int a = 1;
        for (int i = 0; s[i]; i ++) {
            a = a * (s[i] - 'A' + 1) % 47;
        }
        return a;
    }
    char s[11], t[11];
    int main() {
        cin >> s >> t;
        puts( trans(s) == trans(t) ? "GO" : "STAY" );
        return 0;
    }
    

    P1308 统计单词数

    题目链接:https://www.luogu.com.cn/problem/P1308
    题目大意:给定一个单词,请你输出它在给定的文章中出现的次数和第一次出现的位置。注意:匹配单词时,不区分大小写,但要求完全匹配,即给定单词必须与文章中的某一独立单词在不区分大小写的情况下完全相同。
    解题思路:开一个双重 for 循环即可,注意边界处理。
    实现代码如下:

    #include <bits/stdc++.h>
    using namespace std;
    int pos = -1, cnt, n, m;
    char s[15], t[1000010];
    int main() {
        gets(s); gets(t);
        n = strlen(s); m = strlen(t);
        for (int i = 0; i < n; i ++) s[i] = tolower(s[i]);
        for (int i = 0; i < m; i ++) t[i] = tolower(t[i]);
        for (int i = 0; i+n-1 < m; i ++) {
            bool flag = true;
            for (int j = 0; j < n; j ++) if (s[j] != t[i+j]) {
                flag = false;
                break;
            }
            if (flag && ( i == 0 || t[i-1] == ' ' ) && ( i+n==m || t[i+n] == ' ' )) {
                cnt ++;
                if (cnt == 1) pos = i;
            }
        }
        if (pos == -1) cout << pos << endl;
        else cout << cnt << " " << pos << endl;
        return 0;
    }
    

    P1553 数字反转(升级版)

    题目链接:https://www.luogu.com.cn/problem/P1553
    题目大意:讲 小数,分数,百分数,整数 翻转。
    解题思路:根据不同情况讨论,主要就是分隔符号,然后就是整数翻转了。
    实现代码如下:

    #include <bits/stdc++.h>
    using namespace std;
    char s[1010];
    int n, idx = -1, a, b;
    void solve(int L, int R) {
        int i = R, j = L;
        while (i > L && s[i] == '0') i --;
        while (j < i && s[j] == '0') j ++;
        while (i >= j) cout << s[i--];
    }
    int main() {
        cin >> s; n = strlen(s);
        for (int i = 0; i < n; i ++) {
            if (!isdigit(s[i])) {
                idx = i;
                break;
            }
        }
        if (idx == -1) solve(0, n-1);
        else {
            solve(0, idx-1);
            putchar(s[idx]);
            solve(idx+1, n-1);
        }
        return 0;
    }
    

    P1598 垂直柱状图

    题目链接:
    题目大意:输出一个字母数量对应的柱状图。
    解题思路:开一个 cnt 数组用于记录每个字母出现的次数。然后找规律构建输出数据即可。
    实现代码如下:

    #include <bits/stdc++.h>
    using namespace std;
    int cnt[26], maxc;
    char ch[101];
    int main() {
        for (int i = 0; i < 4; i ++) {
            gets(ch);
            int n = strlen(ch);
            for (int i = 0; i < n; i ++) {
                if (ch[i] >= 'A' && ch[i] <= 'Z') {
                    cnt[ ch[i] - 'A' ] ++;
                }
            }
        }
        for (int i = 0; i < 26; i ++) maxc = max(maxc, cnt[i]);
        for (int i = maxc; i >= 1; i --) {
            int n;
            for (int j = 0; j < 26; j ++) if (cnt[j] >= i) n = j;
            for (int j = 0; j <= n; j ++) {
                if (j) putchar(' ');
                putchar(cnt[j] >= i ? '*' : ' ');
            }
            puts("");
        }
        for (int i = 0; i < 26; i ++) {
            if (i) putchar(' ');
            putchar('A' + i);
        }
        puts("");
        return 0;
    }
    

    P1914 小书童——密码

    题目链接:https://www.luogu.com.cn/problem/P1914
    题目大意:恺撒密码的加密。
    解题思路:每个字符移动n位即可,注意使用取模运算,不要一下一下地还原(那样比较慢)。
    实现代码如下:

    #include <bits/stdc++.h>
    using namespace std;
    int n;
    char s[55];
    int main() {
        cin >> n >> s;
        for (int i = 0; s[i]; i ++)
            putchar((s[i] - 'a' + n) % 26 + 'a');
        return 0;
    }
    
  • 相关阅读:
    prometheus client_golang使用
    etcd相关知识
    基于kubernetes v1.17部署dashboard:v2.0-beta8
    浅谈 Linux namespace
    使用kubeadm部署K8S v1.17.0集群
    和我一步步部署 kubernetes 集群
    go语言开发(二)---变量
    Golang学习笔记(一)-Go语言环境安装以及运行代码
    pycharm中设置pylint工具
    keeplive使用
  • 原文地址:https://www.cnblogs.com/quanjun/p/11925130.html
Copyright © 2020-2023  润新知