洛谷 P5587 打字练习

文章介绍了洛谷P5587编程题的解决方案,主要涉及处理输入中的退格键问题以及统计正确字符数。代码示例展示了如何对含有退格键的字符串进行处理,并计算在限定时间内正确的打字数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

洛谷 P5587 打字练习

这题没什么难的,就是逐行处理完空格问题,一一对比统计正确字符数就可以。

但是,这数据也太坑了,范文里也可能出现退格键,这谁想得到啊.....

Code:

#include <iostream>
#include <cstring>

using namespace std;

const int N = 1e4 + 4;

string stan[N], s[N];
int cnt;//范文的行数
int sum;//统计正确的字符数
double t;

//处理退格
void func(string &s)
{
    if(s.find("<") != string::npos)
    {
        int temp = s.find("<");
        while(temp != string::npos)
        {
            if(temp == 0) s = s.substr(1);
            else
            {
                string ss = s;
                s = "";
                if(temp - 2 >= 0) s += ss.substr(0, temp - 1);
                if(temp + 1 < ss.size()) s += ss.substr(temp + 1);
            } 
            temp = s.find("<");
        }
    }
}

int main()
{
    while(getline(cin, stan[cnt]), stan[cnt] != "EOF") cnt ++ ;
    cnt -- ;
    int i = 0;
    while(getline(cin, s[i]), s[i] != "EOF") i ++ ;
    cin >> t;

    for(i = 0; i <= cnt; i ++ )
    {
        //处理退格
        func(stan[i]);
        func(s[i]);

        for(int j = 0; j < min(stan[i].size(), s[i].size()); j ++ )
            if(stan[i][j] == s[i][j]) 
                sum ++ ;
    }

    printf("%.lf\n", sum * 60 / t);

    // system("pause");
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值