Codeforces Round #329 (Div. 2)A. 2Char

本文探讨了如何从包含最多两个不同字母的文章中选择并调整文本,以适应名为2Char的技术期刊格式。通过删除某些单词,确保剩余文本仅使用不超过两个不同字母。目标是在保留文章完整性的前提下,最大化文本总长度,同时考虑到支付与非空格字符数量的关系。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

题意:求只含两个字符以内的字符串的字符个数。
直接暴力
这个简单暴力都搞了我31分钟。还是太渣了!!
读完题之后其实是一个简单题,但是感觉比以前的cf难度大些,想了一下可以直接暴力,但是算了一下怕超时。于是先记录每一种字符的个数,
然后在模拟,枚举两种不同的字符,再看看每个字符串是否只含这两个字符内的字符,如果是加上这类字符串的长度,否者不加。详见代码。
就这个过程思想还没想好就急急忙忙敲代码,浪费了很多时间。
#include
         
          
#include
          
           
#include
           
            
#include
            
             
using namespace std;

const int N = 111;
struct node
{
    int flag[33];
    int total;
}T[N];

char str[N][1001];

int main(void)
{
    int n;
    int i, j;
    scanf("%d", &n);
    memset(T, 0, sizeof(T));
    for(i = 0; i < n; i++)
    {
        scanf("%s", str[i]);
        int len = strlen(str[i]);
        for(j = 0; j < len; j++)
        {
            int tmp = str[i][j] - 'a';
            if(!T[i].flag[tmp])
            {
                T[i].total++;
            }
            T[i].flag[tmp]++;
        }
//        printf("i=%d total=%d\n", i, T[i].total);
//        printf("%d %d %d\n", T[i].flag[0], T[i].flag[1], T[i].flag[2]);
    }
    int ans = 0;
    for(i = 0; i < 26; i++)
    {
        for(j = i+1; j < 26; j++)
        {
            int tmp = 0;
            for(int k = 0; k < n; k++)
            {
                if(T[k].total == 1 && (T[k].flag[i] || T[k].flag[j]))
                {
                    tmp += T[k].flag[i] + T[k].flag[j];
                }
                if(T[k].total == 2 && T[k].flag[i] && T[k].flag[j])
                {
                    tmp += T[k].flag[i] + T[k].flag[j];
                }
            }
            ans = max(ans, tmp);
        }
    }
    printf("%d\n", ans);
    return 0;
}

            
           
          
         

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值