codeforces-593A-2Char

来源:互联网 发布:sodu网站源码 编辑:程序博客网 时间:2024/06/05 18:07

codeforces-593A-2Char


                        time limit per test2 seconds    memory limit per test256 megabytes

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.

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’}.

题目链接:cf-593A

题目大意:以每个串为单位,找出尽可能多的字母,使得这几个串都只包含最多2个相同字母

题目思路:暴力

以下是代码:

#include <vector>#include <map>#include <set>#include <algorithm>#include <iostream>#include <cstdio>#include <cmath>#include <cstdlib>#include <string>#include <cstring>using namespace std;string s[1005];struct node{    char a;    int cnt;}ans[1005][30];int vis[1000];int no[1005];int main(){    int n;    cin >> n;    for (int i = 0; i < n; i++)    {        cin >> s[i];    }     for(int i = 0; i < n; i++)    {        int k = 0;        memset(vis,0,sizeof(vis));        for (int j = 0; j < s[i].size(); j++)        {            if(!vis[s[i][j] - 'a'])            {                ans[i][k].a = s[i][j];  //记录该串的字母                ans[i][k++].cnt++;      //记录该字母的个数                vis[s[i][j] - 'a'] = 1;            }             else            {                if (s[i][j] == ans[i][0].a) ans[i][0].cnt++;                else ans[i][1].cnt++;            }            no[i] = k;  //记录该串有几个字母        }    }    int ret = 0;    for (int i = 'a'; i <= 'z'; i++)    //枚举出现的字母    {        for(int j = i + 1 ; j <= 'z'; j++)        {            int last = 0;            for (int m = 0; m < n; m++)            {                if (no[m] == 2)                {                    if ((ans[m][0].a == i && ans[m][1].a == j ) ||(ans[m][1].a == i && ans[m][0].a == j ))                    {                        last += ans[m][0].cnt;                        last += ans[m][1].cnt;                    }                                   }                if (no[m] == 1)                {                    if ((ans[m][0].a == i || ans[m][0].a == j ))                    {                        last += ans[m][0].cnt;                    }                }            }            ret = max(ret,last);        }    }    cout << ret << endl;    return 0;}
0 0
原创粉丝点击