Skip to main content
  1. Posts/

codeforces #329 div 2 A. 2Char (暴力)

·2 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

A. 2Char

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Sample test(s)

input

4

abb cacc aaa bbb

output

9

input

5

a a bcbcb cdecdecdecdecdecde aaaa

output

6

Note

In the first sample the optimal way to choose words is {‘abb’, ‘aaa’, ‘bbb’}.

In the second sample the word ‘cdecdecdecdecdecde’ consists of three distinct letters, and thus cannot be used in the article. The optimal answer is {‘a’, ‘a’, ‘aaaa’}.

昨天真是智商掉线。

首先对于单词中出现字母超过三个的肯定要排除掉,因为单词并不能分割。

然后用cnt1[]和cnt2[][]两个数组,分别统计当只有一个字母或者两个字母的时候,所有有该字母(或两个字母)的单词,对答案的贡献度(就是单词的长度)

  1/*************************************************************************
  2> File Name: code/cf/#329/A.cpp
  3> Author: 111qqz
  4> Email: rkz2013@126.com
  5> Created Time: 2015年11月05日 星期四 15时02分04秒
  6************************************************************************/
  7
  8#include<iostream>
  9#include<iomanip>
 10#include<cstdio>
 11#include<algorithm>
 12#include<cmath>
 13#include<cstring>
 14#include<string>
 15#include<map>
 16#include<set>
 17#include<queue>
 18#include<vector>
 19#include<stack>
 20#include<cctype>
 21
 22#define lson l,m,rt<<1
 23#define rson m+1,r,rt<<1|1
 24#define ms(a,x) memset(a,x,sizeof(a))
 25using namespace std;
 26const int dx4[4]={1,0,0,-1};
 27const int dy4[4]={0,-1,1,0};
 28typedef long long LL;
 29typedef double DB;
 30const int inf = 0x3f3f3f3f;
 31const int N=105;
 32string st[N];
 33int n;
 34int cnt1[30],cnt2[30][30];
 35int num[30];
 36int main()
 37{
 38#ifndef  ONLINE_JUDGE
 39freopen("in.txt","r",stdin);
 40#endif
 41
 42ms(cnt1,0);
 43ms(cnt2,0);
 44ms(num,0);
 45 45
 46scanf("%d",&n);
 47for ( int i = 0  ; i < n ; i++)
 48{
 49cin>>st[i];
 50int len = st[i].length();
 51ms(num,0);
 52for ( int j =  0 ;  j < len ; j++ )
 53{
 54int tmp =st[i][j]-'a';
 55num[tmp]++;
 56}
 57int sum=0;
 58for ( int j = 0 ; j < 26 ; j++)
 59{
 60if (num[j]) sum++;
 61}
 62if (sum>2) continue;
 63if (sum==1)
 64{
 65int x1 = st[i][0]-'a';
 66cnt1[x1] +=len;
 67}
 68if (sum==2)
 69{
 70int x1 = st[i][0]-'a';
 71int x2;
 72for ( int j = 1 ; j < len ; j++)
 73{
 74int tmp = st[i][j]-'a';
 75if (tmp!=x1)
 76{
 77x2 = tmp;
 78break;
 79}
 80}
 81if (x1>x2) swap(x1,x2);
 82cnt2[x1][x2]+=len;
 83}
 84}
 85int ans = -1;
 86int cur = 0 ;
 87for ( int i = 0 ; i < 25 ; i++)
 88for ( int j = i+1 ; j <26 ; j++ )
 89{
 90cur = cnt1[i]+cnt1[j] + cnt2[i][j];
 91ans = max(ans,cur);
 92}
 93printf("%dn",ans);
 94 94
 95
 96#ifndef ONLINE_JUDGE
 97fclose(stdin);
 98#endif
 99return 0;
100}

Related

codeforces 589 B - Layer Cake

·2 mins
B - Layer Cake **Time Limit:**6000MS **Memory Limit:**524288KB 64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 589B Description Dasha decided to bake a big and tasty layer cake. In order to do that she went shopping and bought n rectangular cake layers. The length and the width of the i-th cake layer were a__i and b__i respectively, while the height of each cake layer was equal to one.