The number of first sieve, and then the frequency of the occurrence of a given character.
#include <cstdio> #include <cstring>using namespace Std;bool pri[2010];char s[2010];void init () {int i,j; memset (pri,1,sizeof pri); pri[0]=pri[1]=0; for (i=2;i<=2000;i++) {if (!pri[i]) continue; for (j=i+i;j<=2000;j+=i) pri[j]=0; }}int Main () {int t,i,cnt[100],f,len,cas=1; Init (); scanf ("%d", &t); while (t--) {scanf ("%s", s); Len=strlen (s); printf ("Case%d:", cas++); memset (cnt,0,sizeof CNT); for (i=0;i<len;i++) {if (s[i]>= ' 0 ' &&s[i]<= ' 9 ') cnt[s[i]-' 0 ']++; else if (s[i]>= ' a ' &&s[i]<= ' Z ') cnt[s[i]-' a ' +10]++; else if (s[i]>= ' a ' &&s[i]<= ' z ') cnt[s[i]-' a ' +36]++; } f=0; for (i=0;i<10;i++) {if (Pri[cnt[i]]) f=1,printf ("%c", i+ ' 0 '); } for (i=10;i<36;i++) {if (Pri[cnt[i]]) f=1,printf ("%c", i-10+ ' A '); } for (i=36;i<62;i++) {if (Pri[cnt[i]]) f=1,printf ("%c", i-36+ ' a '); } if (!f) puts ("empty"); Else puts (""); } return 0;}
1 UVA 10789 Prime Frequency