题解 | #坐标移动#
坐标移动
http://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
不懂正则表达式,按照状态机的思想分析每个字串,贴一下我又臭又长的代码
#include<bits/stdc++.h>
using namespace std;
int main ()
{
string str;
while(cin>>str)
{
vector<int> fenhao;
fenhao.push_back(-1);
int temp=-1;
int flag=0;
map<char,int> move;
move.insert(make_pair('A',0));
move.insert(make_pair('S',0));
move.insert(make_pair('D',0));
move.insert(make_pair('W',0));
while(1)
{
temp=str.find(';',temp+1);
if(temp==-1)
break;
else
fenhao.push_back(temp);
}
fenhao.push_back(str.size());
int cnt=fenhao.size()-1;
for(int i=0;i<cnt;i++)
{
flag=0;
char first;
int second=0;
for(int j=fenhao[i]+1;j<fenhao[i+1];j++)
{
switch(flag)
{
case 0:
{
first=toupper(str[j]);
if(first!='A'&&first!='S'&&first!='D'&&first!='W')
flag=10;
else
flag++;
break;
}
case 1:
{
char next=str[j+1];
if(str[j]<='9'&&str[j]>='0')
{
if(str[j+1]<='9'&&str[j+1]>='0')
second+=(str[j]-'0')*10;
else
{
second+=(str[j]-'0');
flag++;
}
flag++;
}
else
flag=10;
break;
}
case 2:
{
if(str[j]<='9'&&str[j]>='0')
{
second+=(str[j]-'0');
flag++;
}
else
flag=10;
break;
}
default:
{
flag=10;
break;
}
}
if(flag>3)
break;
}
if(flag==3)
{
move[first]+=second;
}
}
cout<<move['D']-move['A']<<","<<move['W']-move['S']<<endl;
}
return 0;
}
查看23道真题和解析