没有想到暴力26个字母组合的情况!因为26*26完全可以暴力,记住
Description
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 Input
4 abb cacc aaa bbb
9
5 a a bcbcb cdecdecdecdecdecde aaaa
6
Sample Output
Hint
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'}.
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stack>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
//#define LOCAL
#define INF 0x3f3f3f3f
#define MAX_N 10000
char SS[101][1005];
struct node {
int let_num, all_num;
int far1_num;//最大两个字母的数量
char far1 , far2;
} word[101];
set<char> tre;
int main()
{
#ifdef LOCAL
freopen("b:\\data.in.txt", "r", stdin);
#endif
int n;
scanf("%d", &n);
for(int i = 0; i < n; i++)
cin >> SS[i];
int word_num = 0;
set<char>::iterator it;
for(int i = 0; i < n; i++)
{
tre.clear();
for(int j = 0; SS[i][j] != '\0'; j++) {
tre.insert(SS[i][j]);
}
if(tre.size() == 1) {
//当只含有一个子母的时候
word[word_num].all_num = strlen(SS[i]);
word[word_num].let_num = 1;
it = tre.begin();
word[word_num].far1 = *it;
word_num++;
}
else
if(tre.size() == 2) {
//当只含有两个字母的时候
word[word_num].all_num = strlen(SS[i]);
it = tre.begin();
word[word_num].let_num = 2;
word[word_num].far1 = *it;
it++;
word[word_num].far2 = *it;
// cout << word[word_num].far1 << word[word_num].far2 << endl;
word_num++;
}
}
//操作取出的只含有两个或者只含有一个子母的数组
//枚举所有的字母组合
int ans = 0;
//枚举是一个子母的情况
for(int i = 97; i < 123; i++) {
char now = (char)i;
int cnt = 0;
for(int i = 0; i < word_num; i++) {
if(word[i].let_num == 1 && word[i].far1 == now) {
cnt += word[i].all_num;
}
}
ans = max(ans, cnt);
}
//枚举是两个字母的情况
for(int i = 97; i < 123; i++ )
{
char fir = (char)i;
for(int j = 97; j < 123; j++) {
char sco = (char)j;
if(fir == sco) continue;
int cnt = 0;
for(int x = 0; x < word_num; x++) {
if(word[x].let_num == 1 && (word[x].far1 == fir || word[x].far1 == sco))
//当出现只有一个子母的单词并且和本操作单词的有一个相等
cnt += word[x].all_num;
else
if(word[x].let_num == 2 && (((word[x].far1 == fir && word[x].far2 == sco) || (word[x].far1 == sco && word[x].far2 == fir))))
{
//当出现两个对应相等的情况
cnt += word[x].all_num;
}
}
ans = max(ans, cnt);
}
}
cout << ans << endl;
return 0;
}