真的就暴漏出来自己的基础知识薄弱了,
所以我才迫切的想将自己的基础打扎实.
贴出代码,我觉得可以用字典树实现呀,,试试咯.
代码:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int t[33];
const int sta = 'a';
int prime (int x)
{
if (x == 1 || x == 0) return 0;
else if (x == 2) return 1;
for (int i = 2; i <= sqrt (x); i++) //这不是坑爹么? 以后千万要注意,这可能坑死人的.
{
if (x % i == 0)
{
return 0;
}
}
return 1;
}
void Solve (char *str)
{
int n = strlen (str);
for (int i = 0; i < n; i++)
{
t[str[i] - sta]++;
}
int min = 10000;
int max = 0;
for (int i = 0; i < 26; i++)
{
if (t[i] > max)
{
max = t[i];
}
if (t[i] < min && t[i] != 0)
{
min = t[i];
}
}
if (prime (max - min))
{
printf ("Lucky Word\n");
cout << max - min << endl;
}
else
{
cout << "No Answer" << endl;
cout << "0" << endl;
}
}
void init ()
{
for (int i = 0; i < 26; i++) //还有初始化的问题.
{
t[i] = 0;
}
}
int main ()
{
int N;
char str[111];
cin >> N;
while (N--)
{
init ();
cin >> str;
Solve (str);
}
return 0;
}