Tarango

CTN A

Sep 23rd, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. //============================================================================
  2. // Name        : Lowest Common Ancestor(logN)
  3. // Author      : Tarango Khan
  4. // Team        : BRACU Byteheads
  5. //============================================================================
  6.  
  7. #include <bits/stdc++.h>
  8. using namespace std;
  9. #define Size 1000005
  10.  
  11. int len;
  12. char s[Size];
  13. int mem[Size][26];
  14. int A[26];
  15.  
  16. int GCD(int a,int b){
  17.     if(b == 0) return a;
  18.     return GCD(b,a%b);
  19. }
  20.  
  21. void preCalc(){
  22.     int pos;
  23.     for(int i = 1;i<=len;i++){
  24.         pos = i-1;
  25.         int idx = (int)(s[pos]-'a');
  26.         for(int j = 0;j<26;j++){
  27.             mem[i][j] = mem[i-1][j];
  28.         }
  29.         mem[i][idx]++;
  30.     }
  31. }
  32.  
  33. bool isPalin(int i,int j){
  34.     int odd = 0;
  35.     for(int c = 0;c<26;c++){
  36.         A[c] = mem[j][c]-mem[i-1][c];
  37.         if(A[c] % 2 != 0) odd++;
  38.     }
  39.     if(odd <= 1) return true;
  40.     return false;
  41. }
  42.  
  43. void solve(){
  44.     int res = 0,dwn = len*(len+1)/2;
  45.     for(int i = 1;i<=len;i++){
  46.         for(int j = i+1;j<=len;j++){
  47.             if(isPalin(i,j) == true) res++;
  48.         }
  49.     }
  50.     printf("res: %d\n",res);
  51.     int gcd = GCD(res,dwn);
  52.     res /= gcd;
  53.     dwn /= gcd;
  54.     printf("%d/%d\n",res,dwn);
  55. }
  56.  
  57. int main(){
  58.     int nCase;
  59.     scanf("%d",&nCase);
  60.     for(int cs = 1;cs<=nCase;cs++){
  61.         scanf("%s",s);
  62.         len = strlen(s);
  63.         for(int i = 0;i<=len;i++){
  64.             for(int j = 0;j<26;j++){
  65.                 mem[i][j] = 0;
  66.             }
  67.         }
  68.         preCalc();
  69.         solve();
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment