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.
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.
Print a single integer — the maximum possible total length of words in Andrew's article.
4 abb cacc aaa bbb
9
5 a a bcbcb cdecdecdecdecdecde aaaa
6
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'}.
题意:给定n个只由小写字母组成的串。让你选择任意两个字母(或者一个字母),找到仅包含该字母的所有串,并统计总长度。
思路:用二维数组len[i][j]记录仅包含字符a(i + 'a' + 1)和b(j + 'a' + 1)的所有串的长度,在输入的过程中维护ans = max(ans, len[i][j] + len[j][i])就可以了。注意只有一个字母时只需维护ans = max(ans, len[i][i])。
AC代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN 50000+10
#define MAXM 50000000
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
using namespace std;
int len[27][27];
char str[1010];
int main()
{
int n; Ri(n);
int ans = 0;
CLR(len, 0);
for(int i = 0; i < n; i++)
{
Rs(str);
int l = strlen(str);
int a = 0, b = 0;
bool flag = true;
for(int j = 0; j < l; j++)
{
if(a == 0)
a = str[j] - 'a' + 1;
else if(a == str[j] - 'a' + 1)
continue;
else if(b == 0)
b = str[j] - 'a' + 1;
else if(b == str[j] - 'a' + 1)
continue;
else
{
flag = false;
break;
}
}
if(flag)
{
if(b)
{
len[a][b] += l;
ans = max(ans, len[a][b] + len[b][a]);
}
else
{
for(int j = 1; j <= 26; j++)
len[a][j] += l, ans = max(ans, a == j ? len[a][j] : (len[a][j] + len[j][a]));
}
}
}
Pi(ans);
return 0;
}