wrench786

Sieve + frequency

Jan 31st, 2022
683
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. bool arr[100001];
  5. int cases;
  6.  
  7. void sieve(){
  8.     arr[0] = true;
  9.     arr[1] = true;
  10.     for(int i=4;i<=100000;i+=2){
  11.         arr[i] = true;
  12.     }
  13.  
  14.     for(int i=3;i*i<=100000;i+=2){
  15.         for(int j = i*i;j<=100000 ;j+=i){
  16.             arr[j] = true;
  17.         }
  18.     }
  19. }
  20.  
  21. void solve(){
  22.     int n;
  23.     string str;
  24.     cin>>n>>str;
  25.  
  26.     int alpha[26]={0};
  27.  
  28.     for(int i=0;i<str.size();i++){
  29.         alpha[str[i]-'A']++;
  30.     }
  31.  
  32.     int co=0;
  33.  
  34.     for(int i=0; i<26;i++){
  35.         if(!arr[alpha[i]]) co++;
  36.     }
  37.     printf("Case %d:\n",++cases);
  38.  
  39.     if(!co) printf("Love is painful !\n");
  40.     else{
  41.        for(int i=0; i<26;i++){
  42.             if(!arr[alpha[i]]) printf("%c = %d\n",i+'A',alpha[i]);
  43.         }
  44.     }
  45. }
  46.  
  47. int main(){
  48.     sieve();
  49.  
  50.     int tc;
  51.     cin>>tc;
  52.     while(tc--){
  53.         solve();
  54.     }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment