0
点赞
收藏
分享

微信扫一扫

信息学奥赛第二节 —— 字符串2

juneyale 2022-04-04 阅读 38
算法

前言

接上次文章,以一道例题开始本文。本题需要读者自行了解一下什么是字典序。在这里简单介绍一下字典序的比较:hello > hellhello < hellp

练习1 原题链接

题目描述

输入

输出

样例输入

样例输出

解题思路:C++中的string类可以直接进行字符串的比较,故建议直接使用'>'、'<'来进行字符串的比较。先输入第一个字符串S1,再输入剩下的字符串S2...Sn,每次输入Si之后都将其与第一个字符串进行比较,若小于第一个字符串,就将其复制给第一个字符串S1,最后输出S1即可。
AC代码

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int n; cin >> n;//输入组数
    string s1; cin >> s1;//输入第一个字符串
    int x = n - 1;//输入剩下的n - 1个字符
    while (x--)
    {
        string s; cin >> s;
        if (s < s1) s1 = s;
    }
    cout << s1 << endl;//s1保存字典序最小的那个字符串
    return 0;
}

字符串中常见的函数


查找、截取:

  • find(sub) —— 查找字符串中子串sub第一次出现的下标,如果没有,则返回-1
  • find(sub,x) —— 在下标x之后查找子字符串sub
  • substr(i,len) —— 从下标i开始,截取长度为len的子串
  • substr(i) —— 从下标i开始截取子串,截取到最后

插入、删除、替换:

  • erase(i,len) —— 从下标i开始删除长度为len个字符
  • erase(i) —— 从下标i开始删除下标i之后的所有字符
  • insert(i,sub) —— 在下标为i的位置插入一个字符串sub
  • replace(i,len,,str) —— 从下标i开始,将其后len个长度的字符替换为str

举例说明

s.find(sub)

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string s = "hello world";
    string x = "world";
    
    int p = s.find(x);//在字符串s中查找子串x,若没有则返回-1
    cout << p << endl;//6
    return 0;
}

substr(i,len)截取world

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string s = "hello world";
    string x = "world";
    
    //第一种方法:
    string res1 = s.substr(6,5);//从字符串s第6个位置开始,向后截取5个字符
    cout << res1 << endl;//world
    
    //第二种方法:
    int p = s.find(" ");//先找到字符串s中空格的位置
    string res2 = s.substr(p + 1,5);//从p + 1位置开始,向后截取5个字符
    cout << res2 << endl;//world
    
    //第三种方法:
    string res3 = s.substr(6);//从第6个位置开始截取到最后
    cout << res3 << endl;//world
    return 0;
}

s.erase、s.insert、s.replace:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string s = "this is a phone";
    
    string res1 = s.substr(5,2);//截取is
    cout << res1 << endl;
    
    s.erase(5,3);//删除is
    cout << s << endl;
    
    s.insert(5,"is ");//插入is
    cout << s << endl;
    
    s.replace(10,5,"book");//替换phone为book
    cout << s << endl;
    return 0;
}

输出:

练习2 原题链接

题目描述

输入

输出

样例输入

样例输出

解题思路:本题可以使用KMP算法来解决。也可以使用上面介绍的几种字符串函数搭配while循环来实现。
AC代码

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string s,t;//s代表子串,t代表模式串
    getline(cin,s);
    getline(cin,t);
    
    int pos = s.find(t);
    if (pos == -1) cout << -1 << endl;//子串不存在
    else//pos != -1:子串存在 
    {
        while (pos != -1) 
        {
            cout << pos + 1 << endl;//题目中下标从1开始
            pos = s.find(t,pos + 1);//从pos + 1的位置开始继续搜索
        }
    }
    return 0;
}

练习3 原题链接

题目描述

输入

输出

样例输入
在这里插入图片描述
样例输出

AC代码

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string s; char c;//读入字符串以及字符
    getline(cin,s); cin >> c;
    
    int p = s.find(c);//先在字符串中找到第一个目标字符出现的位置
    while (p != -1)
    {
        s.erase(p,1);//删除字符
        p = s.find(c);//更新p的位置
    }
    cout << s << endl;
    return 0;
}
举报

相关推荐

0 条评论