There are nn students in the first grade of Nlogonia high school. The principal wishes to split the students into two classrooms (each student must be in exactly one of the classrooms). Two distinct students whose name starts with the same letter will be chatty if they are put in the same classroom (because they must have a lot in common). Let xx be the number of such pairs of students in a split. Pairs (a,b)(a,b) and (b,a)(b,a) are the same and counted only once.
For example, if there are 66 students: “olivia”, “jacob”, “tanya”, “jack”, “oliver” and “jessica”, then:
splitting into two classrooms (“jack”, “jacob”, “jessica”, “tanya”) and (“olivia”, “oliver”) will give x=4x=4 (33 chatting pairs in the first classroom, 11 chatting pair in the second classroom),
splitting into two classrooms (“jack”, “tanya”, “olivia”) and (“jessica”, “oliver”, “jacob”) will give x=1x=1 (00 chatting pairs in the first classroom, 11 chatting pair in the second classroom).
You are given the list of the nn names. What is the minimum xx we can obtain by splitting the students into classrooms?
Note that it is valid to place all of the students in one of the classrooms, leaving the other one empty.
Input
The first line contains a single integer nn (1≤n≤1001≤n≤100) — the number of students.
After this nn lines follow.
The ii-th line contains the name of the ii-th student.
It is guaranteed each name is a string of lowercase English letters of length at most 2020. Note that multiple students may share the same name.
Output
The output must consist of a single integer xx — the minimum possible number of chatty pairs.
Examples
input
Copy
4
jorge
jose
oscar
jerry
output
Copy
1
input
Copy
7
kambei
gorobei
shichiroji
kyuzo
heihachi
katsushiro
kikuchiyo
output
Copy
2
input
Copy
5
mike
mike
mike
mike
mike
output
Copy
4
Note
In the first sample the minimum number of pairs is 11. This can be achieved, for example, by putting everyone except jose in one classroom, and jose in the other, so jorge and jerry form the only chatty pair.
In the second sample the minimum number of pairs is 22. This can be achieved, for example, by putting kambei, gorobei, shichiroji and kyuzo in one room and putting heihachi, katsushiro and kikuchiyo in the other room. In this case the two pairs are kambei and kyuzo, and katsushiro and kikuchiyo.
In the third sample the minimum number of pairs is 44. This can be achieved by placing three of the students named mike in one classroom and the other two students in another classroom. Thus there will be three chatty pairs in one classroom and one chatty pair in the other classroom.
题解:利用了简单组合数。
#include<iostream>
#include <map>
#include<cstring>
using namespace std;
int a[30]={0};
int dp[105];
int c[105];
int main(){
int n;
memset(dp,0x3f,sizeof(dp));
c[0]=0; c[1]=0;c[2]=1; c[3]=3;
for(int i=4;i<=105;i++){
c[i]=c[i-1]+i-1;
}
cin>>n;
string s;
int sum=0;
for(int i=1;i<=n;i++)
{cin>>s;
a[s[0]-'a']++;
}
for(int i=0;i<=100;i++)
{
for(int j=0;j<=i;j++)
{
dp[i]=min(dp[i],c[j]+c[i-j]);
}
}
for(int i=0;i<26;i++)
sum+=dp[a[i]];
cout<<sum<<endl;
return 0;
}