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

摘要:
解决思路:根据不同情况,主要是分隔符号,然后翻转整数。

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

免责声明:文章转载自《洛谷训练新手村之“简单字符串”题解》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇sql server 的临时表和表变量详解sklearn中的make_moons函数下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

php倒序显示中英文字符串

前面我们有讲过,使用php倒序显示字符串。 最后有提到,如果是中文的时候会乱码。 今天分享的就是“解决倒序显示中文字符串而不出现乱码”的问题。 function descstr($str){ $len=strlen($str); //函数返回字符串的长度: $newstr=""; for($i=$le...

Java性能优化之String字符串优化

1.字符串对象及其特点 Java中八大基本数据类型没有String类型,因为String类型是Java对char数组的进一步封装。 String类的实现主要由三部分组成:char数组,offset偏移量,String的长度。 String类型有三个基本特点: 不变性 不变性是指String对象一旦生成,则不能再对它进行改变。 不变性的作用在于当一个对象...

VBS去除字符串的重复项并统计重复字符出现的次数

介绍一下思路: (PS:在这里一定要注意其中的字符串的替换函数replace函数,为了这个我可是折腾了不少时间) 总体来说还是遍历字符串; 思路一: 1遍历字符串A去除字符串A中所有的重复的字符串后的字符,变成非重复的字符串B; 2循环遍历字符串B将所有的字符串B中的单字符与字符串A中的单字符进行比较,如果找到了重复的重复的字符串就就加1,否则不操作; 3...

Python Django 前后端数据交互 之 HttpRequest、HttpResponse、render、redirect

在使用三神装的时候,首先当然是得要导入它们: from django.shortcuts import HttpResponse, render, redirect   一、HttpRequest捕获请求 捕获请求——HttpRequest对象 1、属性 HttpRequest.scheme  #一个字符串,表示请求的方案(通常是http或者https)H...

JS数据类型转换

JS数据类型转换方法主要有三种:转换函数、强制类型转换、利用js变量弱类型转换。 1、转换函数: js提供了parseInt()和parseFloat()两个转换函数。前者把值转换成整数,后者把值转换成浮点数。只有对String类型调用这些方法,这两个函数才能正确运行;对其他类型返回的都是NaN(Not a Number)。  在判断字符串是事是数字值前,...

TCHAR数据类型介绍

转载:https://blog.csdn.net/mousebaby808/article/details/5259944 并不是所有的Windows操作系统都支持UNICODE编码的API(例如早期的Windows98), 这就造成了两种结果:某些版本的Windows应该应用wchar_t来保存字符, 某些平台的Windows应该使用char类型来保存字...