A - 2Char

A - 2Char

Andrew often reads articles in his favorite magazine 2Char. The main feature of these articles is that each of them uses at most two distinct letters. Andrew decided to send an article to the magazine, but as he hasn’t written any article, he just decided to take a random one from magazine 26Char. However, before sending it to the magazine 2Char, he needs to adapt the text to the format of the journal. To do so, he removes some words from the chosen article, in such a way that the remaining text can be written using no more than two distinct letters.

Since the payment depends from the number of non-space characters in the article, Andrew wants to keep the words with the maximum total length.

Input
The first line of the input contains number n (1 ≤ n ≤ 100) — the number of words in the article chosen by Andrew. Following are n lines, each of them contains one word. All the words consist only of small English letters and their total length doesn’t exceed 1000. The words are not guaranteed to be distinct, in this case you are allowed to use a word in the article as many times as it appears in the input.

Output
Print a single integer — the maximum possible total length of words in Andrew’s article.
Examples
Input

4
abb
cacc
aaa
bbb

Output
9

Input
5
a
a
bcbcb
cdecdecdecdecdecde
aaaa

Output
6
--------------------------------------------------------------分割线-------------------------------------------------------------
/考虑到将不同的字母组合表示出来过于复杂,于是考虑把字母转化为 字母-‘a’ 的整数构成的二维数组一一对应,二维数组的值表示两字母组合下的长度,其中单字母字符串增加所有含有该字母组合的长度/

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
double s[26][26]={0},max;  //把26个英文字母两两不同的组合方式转化为二维数组表示
	int n,i,j,k,l,t;    //变量t的值用来表示输入的字符串的不同类型(只有一种字母的字符串   或两种   或更多)
	char a[1000],b,c;    //b,c分别存储字符串出现的两个不同字母
	cin>>n;
	getchar();
	for(i=0;i<n;i++)
	{
		gets(a);
		l=strlen(a);
		t=0;                                         //每输入一个字符串时t重置
		b=a[0];                                        //a【0】字母传给b
		for(j=0;j<l&&t!=2;j++)
		{
			if(a[j]!=b&&t==0){                        //当出现与a【0】不同的字母时把该字母存入c
				t=1;
				c=a[j];                      
			}
			if(a[j]!=b&&a[j]!=c&&t==1) t=2;	          //出现三种及以上字母时t=2
		}
		if(t!=2)                                      //对只含一种或两种字母的字符串进行处理
		{
			if(t==1){                                 //双字母类型处理,对应组合加长度l
				s[b-'a'][c-'a']+=l;
				s[c-'a'][b-'a']+=l;
			}
			else{
				for(j=0;j<26;j++)                  // 单字母类型处理,对所有含该单字母的组合加长度l
				{
				s[b-'a'][j]+=l;
				s[j][b-'a']+=l;
				}
				s[b-'a'][b-'a']-=l;           //仔细观察上面操作有一次重复工作s[b-'a'][b-'a']+=l,进行此操作抵消
			}
		}
	}
	max=s[0][0];                             //找出数组的最大值,工作完毕
	for(i=0;i<26;i++)
	for(j=0;j<26;j++)
	if(s[i][j]>max) max=s[i][j];
	printf("%.0f",max);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值