这道题的意思是说给n个箱子(按照1-n的编号),有m句话,告诉你对的箱子在X号箱子的左边或者右边,问你最终符合条件的箱子有多少个,如果一个都没有输出-1
字符串读入咯~ 记住用getline哟,cin或者%s都是空格截断的~
#include <cstdio>
#include <memory>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
string s;
int n,m,l,r,ans; cin>>n>>m;
l=0,r=n+1,ans=0;
char huiche,dir; scanf("%c",&huiche);
for(int i=0;i<m;i++)
{
getline(cin,s);
int len=s.length()-1,num=0,tlen;
dir=s[7],tlen=len;
//cout<<dir<<endl;
while(s[tlen]!=' ')tlen--;
for(int j=1;j+tlen<=s.length()-1;tlen++)
num= num*10 + s[tlen+j]-'0';
if(dir=='l')
{
r=min(r,num);
if(num<l || num==1) ans=-1;
}
else if(dir=='r')
{
l=max(l,num);
if(num>r || num==n) ans=-1;
}
if(r<l || ans==-1)break;
}
if(ans==-1 || r-l==1) cout<<"-1";
else
{
ans=r-l-1;
cout<<ans;
}
return 0;
}