网址:https://abc104.contest.atcoder.jp/tasks/abc104_b
题目:
Time limit : 2sec / Memory limit : 1024MB
Score : 200 points
Problem Statement
You are given a string S. Each character of S is uppercase or lowercase English letter. Determine if S satisfies all of the following conditions:
- The initial character of S is an uppercase
A
. - There is exactly one occurrence of
C
between the third character from the beginning and the second to last character (inclusive). - All letters except the
A
andC
mentioned above are lowercase.
Constraints
- 4≤|S|≤10 (|S| is the length of the string S.)
- Each character of S is uppercase or lowercase English letter.
#include<iostream> #include<string> #include<cstdio> using namespace std; int n1,t; int main() { string c; int len,f=1; cin>>c; len=c.length(); for(int i=0;i<len;i++) { if(c[i]!='A' && c[i]!='C') if(isupper(c[i])) f=0; } int count=0; for(int i=2;i<len-1;i++) if(c[i]=='C') count++; if(count!=1) f=0; if(c[0]!='A') f=0; if(f==1) cout<<"AC"<<endl; else cout<<"WA"<<endl; return 0; }
解法:首先考虑除了A,C外,是否还有大写字母,如果有,则排除这种情况
-
二是寻找C存在的位置,根据题目已知它是在第三个字符和倒数第二个字符之间的(包含第三和倒数第二个字符)并且次数只能是1,所以这里用了一个count来计数,再判断第一个字符是否是A,就全面考虑了。