[Luogu]P5587 打字练习
题目链接:
https://www.luogu.com.cn/problem/P5587
解题思路:
字符串处理,考虑多种可能情况:
-
范文和输入均可能存在
退格
,没见过范文还带退格的艹 -
计算KPM的时候分母可能为0
-
输入的行数可能比范文的短或长。
输入输出样例
Input1:
hello world.
aaabbbb
x
EOF
heelo world.
aaacbbbb
y<x
EOF
60
Output1:
18
Input2:
<<qwe
EOF
<<qwe
EOF
60
Output2:
3
Input3:
<qwe
EOF
qw<e
EOF
60
Output3:
1
代码
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<cmath>
#include<set>
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
int count(string a,string b){
int ans=0;
int length;
vector<char> c;
vector<char> d;
length=a.length();
for(int i=0;i<length;i++){
if(a[i]=='<' && !c.empty())
c.pop_back();
else{
if(a[i]!='<')
c.push_back(a[i]);
}
}
length=b.length();
for(int i=0;i<length;i++){
if(b[i]=='<' && !d.empty()) d.pop_back();
else{
if(b[i]!='<')
d.push_back(b[i]);
}
}
length = min(c.size(),d.size());
for(int i=0;i<length;++i)
ans+= (c[i]==d[i]);
return ans;
}
int main() {
int n=0;
int i=0;
int time = 0;
double ans=0.0;
string str;
string a[100005];
while(1){
getline(cin,str);
if(str!="EOF") a[n++]=str;
else break;
}
while(1){
getline(cin,str);
if(str!="EOF"){
if(i<n) ans+=count(a[i++],str);
else continue;
}
else
break;
}
cin >> time;
cout << (int)((ans*60/time)+0.5);
}