Codeforces 593 A. 2Char 【Codeforces Round #329 (Div. 2)】

简介:
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 nlines, 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'}.


题目大意:
有 m 行字符串,要求选出最长的一个 2Char 串,2Char穿的定义是最多只有两个字符,也就是说这篇文章中2Char的长度最长,输出最长长度

解题思路:
就是 4 个For语句就行了,复杂度是26*26*1000*100的,不会超时,然后随便搞一下就行了。。。
上代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;

#define MM(a) memset(a,0,sizeof(a))

typedef long long LL;
typedef unsigned long long ULL;
const int maxn = 1e3+5;
const int mod = 1000000007;
const double eps = 1e-10;
const int INF = 0x3f3f3f3f;
LL gcd(LL a, LL b)
{
    if(b == 0)
        return a;
    return gcd(b, a%b);
}
///复杂度26*26*100*1000
char str[maxn][maxn];
int main()
{
    int m;
    while(cin>>m)
    {
        for(int i=0; i<m; i++)
            cin>>str[i];
        int Max = -INF;
        for(char i='a'; i<='z'; i++)
        {
            for(int j='a'; j<='z'; j++)
            {
                int ret = 0;
                for(int ii=0; ii<m; ii++)
                {
                    bool ok = true;
                    for(int jj=0; jj<strlen(str[ii]); jj++)
                    {
                        if(str[ii][jj]!=i && str[ii][jj]!=j)
                        {
                            ok = false;
                        }
                    }
                    if(ok)
                        ret += strlen(str[ii]);
                }
                Max = max(Max, ret);
            }
        }
        cout<<Max<<endl;
    }
    return 0;
}


目录
相关文章
|
27天前
|
人工智能 测试技术 芯片
Codeforces Round 963 (Div. 2)
Codeforces Round 963 (Div. 2)
|
5月前
Codeforces Round #729 (Div. 2)
【6月更文挑战第4天】在这两个编程问题中,B. Plus and Multiply 要求判断通过加法和乘法操作数组是否能形成目标数 `n`。思路是形如 `x^a + yb = n` 的表达式,如果满足则能构造。C. Strange Function 关注的是找到最小正整数 `x` 使得 `x` 不是 `i` 的因子,计算这些值的和模 `10^9+7`。对于每个 `i`,偶数时 `f(i)` 是 3,奇数时是 2,利用因子与最大公约数计算周期性求和。
30 1
【CodeForces】Codeforces Round 857 (Div. 2) B
【CodeForces】Codeforces Round 857 (Div. 2) B
129 0
|
人工智能 BI
Codeforces Round 827 (Div. 4)
Codeforces Round 827 (Div. 4)A~G题解
97 0
Codeforces Round #644 (Div. 3)(A~G)
Codeforces Round #644 (Div. 3)(A~G)
121 0
|
机器学习/深度学习