Luo Gu training Xinshoucun of "simple string" solution to a problem

P1055 ISBN number

Topic links: https://www.luogu.com.cn/problem/P1055
subject to the effect: according to the last digit is correct before nine judges ISBN code.
Problem-solving ideas: a simulation calculation and control number and the last click.
Codes are as follows:

#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] in your flying saucer here, Your Ride Is Here

Link Title: https://www.luogu.com.cn/problem/P1200
Title effect: two strings is calculated in accordance with a conversion rule described in the subject is not the same.
Outline of Solution: Hash write a function for acquiring the value of the string, it is determined whether the Hash value of the two strings are equal.
Codes are as follows:

#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 count the number of words

Topic links: https://www.luogu.com.cn/problem/P1308
subject to the effect: Given a word, you output the number and location of the first occurrence of it appears in a given article. Note: When matching words, is not case sensitive, but requires an exact match, that word must be given with an independent word in the article does not distinguish between identical case-sensitive.
Problem-solving ideas: to open a for loop to a double, attention boundary treatment.
Codes are as follows:

#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 digit reversal (upgrade version)

Topic links: https://www.luogu.com.cn/problem/P1553
subject to the effect: speak decimals, fractions, percentages, integer flip.
Problem-solving ideas: Depending on the discussions, mainly delimiter, then that is an integer overturned.
Codes are as follows:

#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 vertical histograms

Topic links:
Title effect: a number of outputs corresponding to the letter histogram.
Problem-solving ideas: open a cnt array is used to record the number of times each letter appears. Then find the law to build the output data.
Codes are as follows:

#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 small nunnery - Password

Topic links: https://www.luogu.com.cn/problem/P1914
subject to the effect: Caesar encrypted passwords.
Problem-solving ideas: n bits per character can move, pay attention to the use of modulo arithmetic, not about what to restore (as slower).
Codes are as follows:

#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;
}

Guess you like

Origin www.cnblogs.com/quanjun/p/11925130.html