Geek Sundaram and his girlfriend...


Problem Statement :

https://www.codechef.com/ABCS2016/problems/ABACUS02

Problem Solution :

#include<bits/stdc++.h>

using namespace std;

unsigned long long calc[32];

void compute()
{
    int i;
    calc[0]=1;
    for(i=1;i<=30;i++){
        calc[i]=i*calc[i-1];
    }
}

void solve(string s,int n)
{
  vector<int>v;
  int i,c[27]={0};
  for(i=0;i<n;i++){
        if(s[i]<=90)s[i]+=32;
    c[s[i]-'a']++;
  }
  int ans,c1,c2,flag;
  c1=c2=flag=ans=0;
  for(i=0;i<27;i++){
    if(c[i]%2==0){
        ans+=(c[i]/2);
        if((c[i]/2)>1)
        v.push_back(c[i]/2);
    }
    else{
        c2++;
    }
    if(c2>1)
    {
     cout<<"-1"<<endl;
     return;
    }
  }
  unsigned long long sol=calc[ans];
  for(i=0;i<v.size();i++)
    sol/=calc[v[i]];
  cout<<sol<<endl;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    long int t;
    compute();
    long long ans;
    cin>>t;
    string s;
    while(t--){
       cin>>s;
       solve(s,s.length());
    }
    return 0;
}



No comments:

Post a Comment