题目链接:https://vjudge.net/contest/244167#problem/D
题目:
For a dance to be proper in the Altered Culture of Machinema, it must abide by the following rules:
1 A dip can only appear 1 or 2 steps after a jiggle, or before a twirl, as in: • ...jiggle dip... • ...jiggle stomp dip... • ...dip twirl...
2 All dances end with a clap stomp clap.
3 If a dance contains a twirl, it must have a hop.
4 No dance can start with a jiggle.
5 All dances must have a dip.
As instructor at a dance composition school, you must grade many freshman attempts at composing dances. You decide to make an automatic grader that can check against these rules.
1 A dip can only appear 1 or 2 steps after a jiggle, or before a twirl, as in: • ...jiggle dip... • ...jiggle stomp dip... • ...dip twirl...
2 All dances end with a clap stomp clap.
3 If a dance contains a twirl, it must have a hop.
4 No dance can start with a jiggle.
5 All dances must have a dip.
As instructor at a dance composition school, you must grade many freshman attempts at composing dances. You decide to make an automatic grader that can check against these rules.
Input
The input consists of a number of dances, one per line. Each dance has a maximum of 1000 steps. Each step is separated by a single space, and all steps are lowercase alphabetic words at most 100 letters long.
The input consists of a number of dances, one per line. Each dance has a maximum of 1000 steps. Each step is separated by a single space, and all steps are lowercase alphabetic words at most 100 letters long.
Output
If a dance in the input has no mistakes, then the output should contain the words ‘form ok:’ followed by the original composition.
If a dance has a single type of form error, then the output should contain the words ‘form error K:’ where K is the rule which failed, followed by the composition.
If a dance has multiple types of form errors, then the output should contain the errors as a comma separated clause, as in “form errors K(1) K(2) ... K(N −1) and K(N):’ where the form errors are in increasing order, followed by the composition.
If a dance has form error 1, every dip in the dance that violates rule 1 should be printed in upper case.
If a dance in the input has no mistakes, then the output should contain the words ‘form ok:’ followed by the original composition.
If a dance has a single type of form error, then the output should contain the words ‘form error K:’ where K is the rule which failed, followed by the composition.
If a dance has multiple types of form errors, then the output should contain the errors as a comma separated clause, as in “form errors K(1) K(2) ... K(N −1) and K(N):’ where the form errors are in increasing order, followed by the composition.
If a dance has form error 1, every dip in the dance that violates rule 1 should be printed in upper case.
Sample Input
dip twirl hop jiggle hop hop clap stomp clap
dip hop jiggle hop hop clap stomp clap
dip twirl hop jiggle hop hop clap clap stomp
jiggle dip twirl hop jiggle hop hop clap stomp clap
jiggle dip
jiggle
dip twirl hop dip jiggle hop dip hop clap stomp clap
dip hop jiggle hop hop clap stomp clap
dip twirl hop jiggle hop hop clap clap stomp
jiggle dip twirl hop jiggle hop hop clap stomp clap
jiggle dip
jiggle
dip twirl hop dip jiggle hop dip hop clap stomp clap
Sample Output
form ok: dip twirl hop jiggle hop hop clap stomp clap
form error 1: DIP hop jiggle hop hop clap stomp clap
form error 2: dip twirl hop jiggle hop hop clap clap stomp
form error 4: jiggle dip twirl hop jiggle hop hop clap stomp clap
form errors 2 and 4: jiggle dip
form errors 2, 4 and 5: jiggle
form error 1: dip twirl hop DIP jiggle hop dip hop clap stomp clap
题目大意:给你一行跳舞的字符串,由不超过1000个单词组成(仅包含单词和空格),每个单词不超过100个字母,需要满足一下5个要求:
1.“dip”一定要在“jiggle”后面一个单词或两个单词,或者在twirl前面一个单词,其他都是错的。
2.每个字符串的一定要以“clap stomp clap”结尾,否则就是错的。
3.如果该行字符串包含“twirl”,就一定要有“hop”,否则就是错的。
4.该行字符串不能以“jiggle”开头。
5.该行字符串一定要有“dip”。
判断该行字符串是否有错,如果没错就直接输出 ‘form ok:’加上输入的那行字符串,如果有错,就输出那么错误的编号,再加上输入的那行字符串,注意,如果是错误1的话,需要将错误的dip改成大写的DIP,具体输出格式看样例。
解题思路:这题目有点水,当时比赛时看起来好像挺麻烦的就没去做,其实那几个条件判断都不是很复杂。也不用想啥思路的,直接就是输入后一个一个条件判断就是了。输入一行可以用getline()输入,然后将每个单词分开就行。条件判断有点复杂的就是条件1吧,开始都判断错了,可以直接判断符合该条件的所有情况,反过来就是不符合该条件的情况。然后这个题目最变态的就是这输出格式了,弄的那么复杂,还是要挺小心的。代码有点长,码力还比较弱,码了挺久的,开始样例都没过,改错改来改去的,还好一次提交就过了。。
附上代码:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<vector> 5 #include<cmath> 6 #include<algorithm> 7 using namespace std; 8 string str; 9 int flag[6]; 10 11 int main() 12 { 13 while(getline(cin,str)) 14 { 15 vector<string> vec; 16 memset(flag,0,sizeof(flag)); 17 for(int i=0;i<1000;i++) 18 vec.clear(); 19 int len=str.length(); 20 string ss; 21 for(int i=0;i<len;i++) 22 { 23 if(str[i]!=' ') 24 { 25 ss+=str[i]; 26 } 27 if(str[i]==' '||i==len-1) 28 { 29 vec.push_back(ss); 30 ss=""; 31 } 32 } 33 //判断条件2 34 if(str.size()<15) 35 flag[2]=1; 36 else 37 { 38 string sss=str.substr(str.size()-15,15); 39 //cout<<sss<<endl; 40 if(sss!="clap stomp clap") 41 flag[2]=1; 42 } 43 //cout<<flag[2]<<endl; 44 str.clear(); 45 int l=vec.size(),cntdip=0,cnthop=0,cnttwirl=0; 46 for(int i=0;i<l;i++) 47 { 48 //cout<<vec[i]<<endl; 49 if(vec[0]=="jiggle") //判断条件4 50 { 51 flag[4]=1; 52 } 53 if(vec[i]=="twirl") 54 cnttwirl++; 55 if(vec[i]=="hop") 56 cnthop++; 57 if(vec[i]=="dip") //判断条件1 58 { 59 cntdip++; 60 int flag1=0,flag2=0,flag3=0; 61 if(i>=1&&vec[i-1]=="jiggle") 62 flag1=1; 63 if(i>=2&&vec[i-2]=="jiggle") 64 flag2=1; 65 if(i<=l-2&&vec[i+1]=="twirl") 66 flag3=1; 67 if(flag1==0&&flag2==0&&flag3==0) //全都不符合说明是错的 68 { 69 flag[1]=1; 70 vec[i]="DIP"; 71 } 72 } 73 } 74 if(cnttwirl!=0&&cnthop==0) //判断条件3 75 flag[3]=1; 76 if(cntdip==0) //判断条件5 77 flag[5]=1; 78 int cnt=0,k; 79 for(int i=1;i<=5;i++) 80 { 81 if(flag[i]) 82 { 83 cnt++; 84 k=i; 85 } 86 } 87 if(cnt==0) 88 cout<<"form ok: "; 89 else if(cnt==1) 90 printf("form error %d: ",k); 91 else 92 { 93 int k=0; 94 printf("form errors "); 95 for(int i=1;i<=5;i++) 96 { 97 if(flag[i]) 98 { 99 k++; 100 if(k==cnt-1) 101 printf("%d and ",i); 102 else if(k==cnt) 103 printf("%d: ",i); 104 else 105 { 106 printf("%d, ",i); 107 } 108 } 109 } 110 } 111 for(int i=0;i<l;i++) 112 { 113 if(i==0) 114 cout<<vec[i]; 115 else 116 cout<<" "<<vec[i]; 117 } 118 cout<<endl; 119 } 120 return 0; 121 }