P1125 [NOIP2008 提高组] 笨小猴


题目传送门

#include 

using namespace std;
const int N = 30;
const int INF = 0x3f3f3f3f;

int a[N];
string s;
int maxn = -INF, minn = INF;

bool IsPrime(int n) {
    if (n < 2) return false;
    for (int i = 2; i <= n / i; i++)
        if (n % i == 0) return false;
    return true;
}

int main() {
    cin >> s;
    //计数
    for (int i = 0; i < s.size(); i++) a[s[i] - 'a']++;

    //这道题需要注意的是:
    //最小值判断需要花点心思,因为如果按照传统办法,那么必须MinNum=0,而本题题意是找出出现过的最少字符!!!所以,需要非0的最小值!注意!
    for (int i = 0; i < 26; i++) {
        maxn = max(maxn, a[i]);
        if (a[i]) minn = min(minn, a[i]);
    }

    if (IsPrime(maxn - minn)) {
        printf("Lucky Word\n");
        printf("%d", maxn - minn);
    } else {
        printf("No Answer\n");
        printf("0");
    }

    return 0;
}