Codeforce 593A 思维+枚举字符

本文探讨了一种算法,用于从一系列字符串中选取满足特定条件的子集,目标是在不超过两种不同字符的前提下,最大化字符串总长度。通过遍历所有可能的字符组合,找到最优解。

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

Examples

input

Copy

4
abb
cacc
aaa
bbb

output

Copy

9

input

Copy

5
a
a
bcbcb
cdecdecdecdecdecde
aaaa

output

Copy

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

题目大意:给你n个字符串,从中选择可以多个字符串,前提条件是选择的字符串里面最多有连个不同的小写字母,求最长的这样的字符串的长度

 

这道题我被一组数据卡了半天,就是2  aaaa vvvv 我输出的是4,但是AC的答案应该是8,还是方法不对,看了题解才知道,原来可以枚举小写字母,每次枚举两个就可以了,因为题目要求的是最多有两个不同。具体看代码,算是学到一种骚操作了

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;i++)
int n;
char str[110][1010];
int main()
{
    int ans=0;
    int temp=0;
    scanf("%d",&n);
    rep(i,0,n-1)
    scanf("%s",str[i]);
    for(char i='a';i<='z';i++)
        for(char j=i+1;j<='z';j++)
    {
        temp=0;
        for(int k=0;k<n;k++)
        {
        int len1=strlen(str[k]);
        int l;
        for(l=0;l<len1;l++)
        if(str[k][l]!=i&&str[k][l]!=j) break;
        if(l==len1)
        temp+=len1;
        }
        ans=max(ans,temp);
    }
    printf("%d",ans);
}

 

### Codeforces 题目难度分布及评级标准 Codeforces 的题目难度范围广泛,涵盖了从新手到专家级别的各种挑战[^1]。该平台通过细致的分层机制来区分不同难度等级的问题,使得每位参赛者都能找到适合自己水平的任务。 #### 评分体系概述 Codeforces 使用基于积分制的评价系统,其中每道题都有一个对应的分数(rating),用于表示其相对难易程度。较低的 rating 表明这是一道较为简单的题目;而较高的 rating 则意味着更高的复杂性和解决难度。通常情况下: - **简单题**:Rating 小于等于 1200 分 - **中等偏难题**:Rating 范围大约在 1600 至 2000 分之间 - **困难题**:Rating 大于等于 2400 分 这些数值并不是固定的界限,而是根据社区反馈以及比赛实际情况调整的结果。 #### Divisions 和 Contest Types 为了更好地适应不同程度的学习者和技术爱好者的需求,Codeforces 提供了多种类型的竞赛活动,比如 Division 1, Division 2, Division 3 及 Division 4 等不同类型的比赛[^2]。每个 division 对应着不同的最低准入门槛——即参与者应该具备的基础能力或经验水平。例如,在更高级别的比赛中会遇到更多高难度的问题。 #### 技术领域覆盖 平台上发布的题目不仅限于单一的技术方向,还涉及到多个计算机科学的重要分支,如贪心算法、动态规划、图论等领域。这种多样性有助于全面锻炼编程技巧并促进跨学科思维的发展。 ```python # 示例 Python 代码片段展示如何获取某场比赛的信息 import requests def get_contest_info(contest_id): url = f"https://codeforces.com/api/contest.standings?contestId={contest_id}&from=1&count=1" response = requests.get(url).json() if 'result' not in response or 'problems' not in response['result']: return None problems = response['result']['problems'] for problem in problems: print(f"{problem['index']}: {problem['name']} - Rating: {problem.get('rating', 'N/A')}") get_contest_info(1669) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值