A certain ICPC regional contest has specific requirements for the names of the competing teams.
One of the reasons this is done is so spectators can determine which school each team is from. The
requirements for a team name are specified on the website for the region as shown here:
• Each team name must consist of the institution name (greater than 1 character but less than
or equal to 8 characters) followed by a hyphen (-) followed by a team name (greater than 0
characters but less than or equal to 24 characters). Please note that spaces are counted as
characters.
• Format: INSTITUTION-TEAMNAME
INSTITUTION = University name or abbreviation. 1 < length(INSTITUTION ) <= 8.
TEAMNAME = Your team name. 1 <= length(TEAMNAME) <= 24.
Write a program that determines if a supplied string is a valid team name for this region.
Input
The input consists of a single line containing a string of characters at least one character long and no
more than 80 characters long. The input string will not have any leading or trailing spaces. The
INSTITUTION may not contain hyphens (obviously). The TEAMNAME, however, may contain
hyphens.
Output
The single output line consists of the word YES if the string represents a valid team name for the
region or NO if the string does not represent a valid team name for the region.
Sample 1:
Stevens-1
YES
Sample 2:
INSTITUTION-TEAMNAME
NO
Sample 3:
Dragons
NO
Sample 4:
VIRGTECH - Eagles
NO
“Please note that spaces are counted as characters.”
空格也可以作为字符输入!!!
ac代码:
#include<iostream>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<set>
#include<numeric>
#include<vector>
#include<queue>
#include<array>
#define _USE_MATH_DEFINES
using namespace std;
typedef long long ll;
const int mod=10^9+7;
//tuple<int,int,int>p[100000]
int main()
{
string a;
getline(cin,a);
int len=a.size();
// cout<<len<<endl;
int ins=0;
int group=0;
for(int i=0;i<len;i++)
{
if(a[i]=='-')
{
ins=i;
break;
}
}
//cout<<ins<<endl;
group=len-ins-1;
//cout<<group<<endl;
if((ins>1&&ins<=8)&&(group>=1&&group<=24))
cout<<"YES";
else
cout<<"NO";
return 0;
}
错误代码:
#include<iostream>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<set>
#include<numeric>
#include<vector>
#include<queue>
#include<array>
#define _USE_MATH_DEFINES
using namespace std;
typedef long long ll;
const int mod=10^9+7;
//tuple<int,int,int>p[100000]
int main()
{
string a;
cin>>a;
int len=a.size();
// cout<<len<<endl;
int ins=0;
int group=0;
for(int i=0;i<len;i++)
{
if(a[i]=='-')
{
ins=i;
break;
}
}
//cout<<ins<<endl;
group=len-ins-1;
//cout<<group<<endl;
if((ins>1&&ins<=8)&&(group>=1&&group<=24))
cout<<"YES";
else
cout<<"NO";
return 0;
}
把cin换成getline就对了