#include<bits/stdc++.h>
using namespace std;
int main()
{
//加快cin的效率
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
string s,t;
while(getline(cin,s))
{
stringstream ss(s);
pair<int,int> p(0,0);//初始值
while(getline(ss,t,';'))
{
if(t.empty())
{
continue;
}
string _ = t.substr(1);
if(regex_match(_, regex("[0-9]+")))
{
switch (t[0])
{
case 'A':
p.first -= stoi(_);
break;
case 'D':
p.first += stoi(_);
break;
case 'W':
p.second += stoi(_);
break;
case 'S':
p.second -= stoi(_);
}
}
}
cout << p.first << "," << p.second << endl;
}
return 0;
}
1、加快cin 的速度
ios::sync_with_stdio(false);
std::ios::sync_with_stdio(false);,这样就可以取消cin于stdin的同步了
cin.tie(0),cout.tie(0);
tie(0)(0表示NULL)来解除cin与cout的绑定,进一步加快执行效率
2、getline(ss,t,‘;’)
getline是从流中获取一行信息输入到字符串中:
从is中提取字符并将其存储到str中,直到找到定界字符delim(或换行字符“n”,对于(2))。
3、regex_match(,regex(“[0-9]*”) 需要更正为 regex_match(,regex(“[0-9]+”) 理由:[0-9]+ 表示必须出现一次数字, 而不是[0-9]* 不出现也可以,否则 string _ 会出现为空的情况,导致 stoi(_) 出错
参考连接:https://blog.nowcoder.net/n/f4bfcd09267147f3ac4d99b28d1d26be?f=comment