题解 CF284B 【Cows and Poker Game】

这题一开始我还想着暴力,结果暴力刚刚打出来,TLE:

#include <iostream>
#include <cstring>
using namespace std;

char s[200005];

int main()
{
	int n, ans = 0;
	cin >> n >> s;
    n--;
	for(int i = 0; i <= n; i++)
	{
		if(s[i] == 'A' || s[i] == 'I')
		{
			bool flag = true;
			for(int j = 0; j <= n; j++)
			{
				if(i != j)
				{
					if(s[j] == 'I')
					{
						flag = false;
						break;
					}
				}
			}
			if(flag && n != 1)
			{
				ans++;
			}
		}
	}
	cout << ans << endl;
	return 0;
}

唉,突然想到可以思路:

统计i和a的数量,如果说i比1大,也就是大于等于2,那么所有奶牛都会被i看到,就是0

如果i为1,那就是自己一个人,1

不然就输出a的数量:

#include <iostream>
#include <cstring>
using namespace std;

string s;

int main()
{
	int n, ans = 0, icount = 0, acount = 0;
	cin >> n >> s;
    n--;
	for(int i = 0; i <= n; i++)
	{
		if(s[i] == 'I')
		{
			icount++;
		}
		else if(s[i] == 'A')
		{
		    acount++;
        }
	}
	if(icount > 1)
	{
	    cout << "0\n";
    }
    else
    {
        cout << (icount == 1 ? 1 : acount) << endl;
    }
	return 0;
}
posted @ 2020-10-27 20:27  HappyBobb  阅读(9)  评论(0)    收藏  举报  来源