【題解】ZeroJudge d107: NOIP 2008 1.笨小猴

```#include <iostream>
#include <cstring>
using namespace std;
int a[26];
int prime[100];

int main() {
// 建立質數表（单词长度小于100）
for (int i=0; i<100; i++) prime[i] = 1;
prime[0] = 0;
prime[1] = 0;
int p = 2;
while (p*p <= 100){
if (prime[p]){
for (int i=p*p; i<100; i+=p){
prime[i] = 0;
}
}
p++;
}
string s;
int mn, mx;
while (cin >> s){
// 单词只可能出现小写字母
// a[26] 存放每個字母出現的次數
memset(a, 0, sizeof(a));
for (int i=0; i<s.size(); i++){
a[s[i] - 'a']++;
}
mn = 100;
mx = 0;
for (int i=0; i<26; i++){
if (a[i] != 0){
mn = min(mn, a[i]);
mx = max(mx, a[i]);
}
}
if (prime[mx - mn]){
cout << "Lucky Word\n";
cout << mx - mn << '\n';
}else{
cout << "0\n";
}
}
return 0;
}
```

Python code (credit: Amy Chou)

```# 建立質數表（单词长度小于100）
prime = [1 for _ in range(100)]
prime[0] = 0
prime[1] = 0
for i in range(2, 100):
if prime[i]:
for j in range(i+i, 100, i):
prime[j] = 0

while True:
try:
s = input()
# 单词只可能出现小写字母
a = [0 for _ in range(26)]
for c in s:
a[ord(c.lower()) - ord('a')] += 1

mn = 100
mx = 0
for i in range(26):
if a[i] > 0:
mn = min(mn, a[i])
mx = max(mx, a[i])

if prime[mx - mn] == 1:
print("Lucky Word")
print(mx - mn)
else: