2010年9月17日 星期五

ACM 10789 - Prime Frequency

#include <stdio.h>
#include <string.h>
#include <memory.h>
struct Num
{
char word;
int count;
};

int isPrime (int n);
void insert (char ch);
int checkRepeat (char ch);
int change (int i, int j);
struct Num N[201];
int repeatIndex = 0, index = 0;
int main()
{
int n, i, j, isPut;
char str[2001], ch;
scanf("%d", &n);
for (i = 0; i < n ; i ++)
{
index = 0, isPut = 0;
memset(str, 0, strlen(str));
scanf("%s", str);
for (j = 0;j < strlen(str); j ++)
insert(str[j]);
printf("Case %d: ", i + 1);
for (j = 0;j < index;j ++)
if (isPrime(N[j].count)) printf("%c", N[j].word), isPut = 1;
if (isPut != 1) printf("empty");
printf("\n");
}

return 0;
}

void insert (char ch)
{
int i;
if ( !checkRepeat(ch))
{
N[index].word = ch, N[index].count = 1, index ++;
for (i = index ; i >= 2; i--)
if ( !change(i - 1, i - 2)) break;
}
else
N[repeatIndex].count ++;
}

int checkRepeat (char ch)
{
for (repeatIndex = 0 ; repeatIndex < index; repeatIndex ++)
{
if (N[repeatIndex].word == ch)
return 1;
}
return 0;
}

int change (int i, int j)
{
struct Num temp;
int k = (int)N[i].word, h = (int)N[j].word;
if (N[i].word < N[j].word )
{
temp = N[i], N[i] = N[j], N[j] = temp;
return 1;
}
return 0;
}

int isPrime (int n)
{
int i;
if (n == 1) return 0;
for (i = 2; i < n; i ++)
if (n % i == 0) return 0;
return 1;
}


回目錄
回首頁

沒有留言 :

張貼留言

Related Posts Plugin for WordPress, Blogger...