题目:NEUQ
样例:
AC代码考场版:
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
char sc[] = {'0','0','0','0','0'};
int sum, cnt;
int main(void)
{
int n;
cin>> n;
sum = n;
char c;
for(int i = 1; i <= n; i++)
{
cin>> c;
if(sc[1] == '0' && c == 'N')
sc[1] = 'N';
if(sc[1] == 'N' && sc[2] == '0' && c == 'E')
sc[2] = 'E';
if(sc[1] == 'N' && sc[2] == 'E' && sc[3] == '0' && c == 'U')
sc[3] = 'U';
if(sc[1] == 'N' && sc[2] == 'E' && sc[3] == 'U' && sc[4] == '0' && c == 'Q')
{
sc[4] = 'Q';
for(int j = 1; j <= 4; j++)
sc[j] = '0';
cnt += 4;
}
}
cout<< sum - cnt << endl;
return 0;
}
从大佬的代码中找了几个。优雅,太优雅了,贴在这了。
AC代码优化版(更加清晰了些):
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
string str;
cin>>str;
string str2="NEUQ";
int cnt=0,ans=0;
for(int i=0;i<n;i++)
{
if(str[i]==str2[cnt]) cnt++;
else ans++;
if(cnt==4) cnt=0;
}
if(cnt!=0) ans+=cnt;
cout<<ans;
return 0;
}
双重循环。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e6 + 10;
int n;
char str[N];
char s[] = {'N', 'E', 'U', 'Q'};
int main()
{
scanf("%d%s", &n, str);
int cnt = 0, j = 0;
for(int i = 0; i < n; i ++)
{
if(str[i] == s[j])
{
j ++;
if(j == 4) cnt ++, j = 0;
}
}
printf("%d\n", n - 4 * cnt);
return 0;
}
AC代码(特别注意注释的循环)
#include <iostream>
#include <string>
using namespace std;
int main()
{
int n;
cin>>n;
string s;
string t = "NEUQ";
cin>>s;
int idx = 0,count = 0;
for(int i = 0; s[i]; i++) //字符串最后是\0
{
if(s[i]==t[idx])
{
idx++;
if(idx==4)
{
idx = 0;
count++;
}
}
}
cout<<n-4*count;
return 0;
}
欢迎交流~ 祝大家天天快乐AC~