Codeforces Round 933 (Div. 3) C 题

C. Rudolf and the Ugly String

Rudolf has a string ss of length nn. Rudolf considers the string s to be ugly if it contains the substring† "pie" or the substring "map", otherwise the string s will be considered beautiful.

For example, "ppiee", "mmap", "dfpiefghmap" are ugly strings, while "mathp", "ppiiee" are beautiful strings.

Rudolf wants to shorten the string s by removing some characters to make it beautiful.

The main character doesn't like to strain, so he asks you to make the string beautiful by removing the minimum number of characters. He can remove characters from any positions in the string (not just from the beginning or end of the string).

 String a is a substring of b if there exists a consecutive segment of characters in string b equal to a.

Input

The first line contains a single integer t (1≤t≤104) — the number of test cases. The descriptions of the test cases follow.

The first line of each test case contains a single integer n(1≤n≤106) — the length of the string s.

The next line of each test case contains the string s of length n. The string ss consists of lowercase Latin letters.

The sum of nn over all test cases does not exceed 106.

 

Output

For each test case, output a single integer — the minimum number of characters that need to be deleted to make the string s beautiful. If the string is initially beautiful, then output 0.

题意: 

对于给定字符串s,你可以对其中的任意位置进行删除,使的s的子串中不含有"map",和"pie", 对于map和pie ,你只需要删除一个位置即可改变,但请尤其注意"mapie", 其既含有“map”,有含有“pie”,因此,相当于ans加了两次,在map时加了一次,在pie时加了一次,所以在遇到mapie时要ans--;

substr 函数:

substr 是 C++ 标准库中 std::string 类的成员函数,用于从一个字符串中提取子串。

cpp

std::string substr(size_t pos = 0, size_t count = npos) const;

参数说明

  • pos:子串的起始位置,即从原始字符串中的哪个索引开始提取,默认为 0。
  • count:子串的长度,即要提取的字符个数。默认值 npos 表示提取从起始位置 pos 到字符串末尾的所有字符。

返回值

  • 返回一个 std::string 对象,包含从原始字符串中提取的子串。

 代码实现:

#include<bits/stdc++.h>

using namespace std;

void solve()
{
    int n; cin>>n;
    string s; cin>>s;

    int ans=0;

    for(int i=0;i<n;i++)
    {
        if(i+3<=n && s.substr(i,3)=="map")
        {
            ans++;
        }
        if(i+3<=n && s.substr(i,3)=="pie")
        {
            ans++;
        }
        if(i+5<=n && s.substr(i,5)=="mapie")
        {
            ans--;
        }
    }
    cout<<ans<<endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin>>t;

    while(t--)
    {
        solve();
    }
    return 0;
}

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Codeforces Round 894 (Div. 3) 是一个Codeforces举办的比赛,是第894轮的Div. 3级别比赛。它包含了一系列目,其中包括目E. Kolya and Movie Theatre。 根据目描述,E. Kolya and Movie Theatre问要求我们给定两个字符串,通过三种操作来让字符串a等于字符串b。这三种操作分别为:交换a中相同位置的字符、交换a中对称位置的字符、交换b中对称位置的字符。我们需要先进行一次预处理,替换a中的字符,然后进行上述三种操作,最终得到a等于b的结果。我们需要计算预处理操作的次数。 根据引用的讨论,当且仅当b[i]==b[n-i-1]时,如果a[i]!=a[n-i-1],需要进行一次操作;否则不需要操作。所以我们可以遍历字符串b的前半部分,判断对应位置的字符是否与后半部分对称,并统计需要进行操作的次数。 以上就是Codeforces Round 894 (Div. 3)的简要说明和目E. Kolya and Movie Theatre的要求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Codeforces Round #498 (Div. 3) (A+B+C+D+E+F)](https://blog.csdn.net/qq_46030630/article/details/108804114)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Codeforces Round 894 (Div. 3)A~E解](https://blog.csdn.net/gyeolhada/article/details/132491891)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值