信息学奥赛第二节 —— 字符串2(s.find() + s.substr() + s.erase() + s.insert() + s.replace())

前言

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

练习1 原题链接

题目描述

编写程序,针对输入的N个不同的字符串,输出其中字典码最小的字符串。

输入

输入第一行给出正整数N;随后N行,每行给出一个长度小于80的非空字符串,其中不会出现换行符,空格,制表符。

输出

输出字典码最小的字符串

样例输入

5
Li
Wang
Zha
Jin
Xian

样例输出

Jin

解题思路: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;
}

输出:

is
this a phone
this is a phone
this is a book

练习2 原题链接

题目描述

请问在一个父字符串s中是否存在子字符串t。如果存在,则输出子字符串t在父字符串中所有的起始位置,如果不存在,则输出-1。
比如:假设父字符串s = “Go Abc good goole!”,子字符串t = “go”,那么输出位置:
8
13
再比如:假设父字符串s = “Go Abc good goole!”,子字符串t = “hi”,那么输出结果:-1。

输入

第一行输入父字符串的值;
第二行输入子字符串的值;

输出

输出子字符串在父字符串中所有的位置,如果父字符串中不存在子字符串,请输出-1。

样例输入

Go Abc good goole!
go

样例输出

8
13

解题思路:本题可以使用KMP算法来解决。也可以使用上面介绍的几种字符串函数搭配while循环来实现。需要注意的是:在while循环中,如果写成pos = s.find(t,pos);则会TLE
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 原题链接

题目描述

从键盘输入一个字符串str和一个字符c,删除str中的所有字符c并输出删除后的字符串str。

输入

第一行是一个字符串; (不含空格)
第二行是一个字符。

输出

删除指定字符后的字符串。

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

sdfsdf

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

本题的易错点:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>

using namespace std;

int main()
{
    string s; getline(cin,s);
    char c; cin >> c;
    
    int len = s.size();
    for (int i = 0 ;i < len;i++)
        if (s[i] == c) 
            s.erase(i,1);
            
    cout << s << endl;
    return 0;
}

以上代码是错误的,原因是s.erase(i,1); 这个是函数,erase删除之后这个字符串的长度就变了。所以正确的思路是每次查找要删除的字符的位置,然后删除,直到找不到为止。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值