洛谷 / 题单列表 / 题单详情 【入门5】字符串

【深基6.例6】文字处理软件 - 洛谷

#include<bits/stdc++.h>
using namespace std;
string str, s;
int N, M, K, A, B;
int main()
{
    cin >> N >> str;
    while (N--)
    {
        cin >> M;
        if (M == 1)
        {
            cin >> s;
            str += s;
        }
        else if (M == 2)
        {
            cin >> A >> B;
            str = str.substr(A, B);
        }
        else if (M == 3)
        {
            cin >> K >> s;
            str = str.substr(0, K) + s + str.substr(K);
        }
        else
        {
            cin >> s;
            auto it = str.find(s);
            if (it == str.npos)cout << -1 << endl;
            else cout << it << endl;
            continue;
        }
        cout << str << endl;
    }
    return 0;
}

[NOIP2011 普及组] 统计单词数 - 洛谷

#include<bits/stdc++.h>
using namespace std;
string s, ans;
char ch;
int main()
{
    getline(cin, ans);
    getline(cin, s);
    for (auto& i : ans)
        if (i >= 'a' && i <= 'z')
            i ^= 32;
    for (auto& i : s)
        if (i >= 'a' && i <= 'z')
            i ^= 32;
    ans = " " + ans + " ";
    s = " " + s + " ";
    if (s.find(ans) == s.npos)
        cout << "-1";
    else
    {
        int sum = 0;
        auto pos = s.find(ans);
        while (pos != s.npos)
        {
            sum++;
            pos = s.find(ans, pos + 1);
        }
        cout << sum << " " << (int)s.find(ans);
    }
    return 0;
}

honoka的键盘 - 洛谷

删除对应子串比如改变子串

#include<bits/stdc++.h>
using namespace std;
string str;
int sum, N, falg;
int main()
{
    cin >> N >> str;
    auto pos = str.find("VK");
    if (pos == str.npos)
        cout << (str.find("KK") != str.npos || str.find("VV") != str.npos) << endl;
    else
    {
        for (int i = 0; i != str.size(); i++)
        {
            if (str[i] == 'V' && str[i + 1] == 'K')
            {
                str[i] = str[i + 1] = 'v';
                sum++;
            }
        }
        cout << sum + (str.find("KK") != str.npos || str.find("VV") != str.npos) << endl;
    }
    return 0;
}

P1553 数字反转(升级版)

数字反转(升级版) - 洛谷

#include<bits/stdc++.h>
using namespace std;
string s1, s2;
char ch;
bool is_BFH, is_XS, is_FH;
int main()
{
	while (cin >> ch)
	{
		if (ch == '.') { is_XS = true; continue; }
		if (ch == '%'){is_BFH = true; continue;}
		if (ch == '/'){is_FH = true; continue;}
		if (!is_XS && !is_FH)s1 += ch;
		else s2 += ch;
	}
	reverse(s1.begin(), s1.end());
    if(s1.size() != 1)
	while (s1[0] == '0')s1.erase(0, 1);
    if(s2.size() != 1)
	while (s2[0] == '0')s2.erase(0, 1);
    reverse(s2.begin(), s2.end());
    if(s2.size() != 1)
	while (s2[0] == '0')s2.erase(0, 1);
	if (is_XS)
		cout << s1 << '.' << s2;
	else if (is_BFH)
		cout << s1 << '%';
	else if (is_FH)
		cout << s1 << '/' << s2;
	else
		cout << s1;
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值