2Char(字符串处理问题)

来源:互联网 发布:大淘客cms建站要钱吗? 编辑:程序博客网 时间:2024/05/12 21:22
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
4abbcaccaaabbb
output
9
input
5aabcbcbcdecdecdecdecdecdeaaaa
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'}.


思路:
   就是要求字符串的两个字母组成的有多少个串的问题,一开始没想到直接固定一个串去求一个串是错误的,所以Wa了。因为
aaa
abb
acc
这三个串如果是选第一个串的话就是九个字母了,但是超了两个字符,所以后来想到了我每次都是要两个字母,不如直接固定两个字母然后再去扫每个串,符合就加起来,这样子就OK了。

AC代码:

#include<iostream>#include<algorithm>#include<cstring>#include<string>#include<vector>#include<cstdio>#include<cmath>using namespace std;#define CRL(a) memset(a,0,sizeof(a))typedef __int64 ll;#define T 1000005int s[200][27];bool jugde(int x,int y,int t){    int c=0;    for(int i=0;i<26;++i){        if(s[t][i])            if(i!=x&&i!=y)return false;    }    return true;}int main(){  /*  freopen("input.txt","r",stdin);*/    int n,i,j,c,tmp,cnt,ma;    bool vis[200],v[26];    string str;    while(cin >> n)    {         ma = 0;        memset(s,0,sizeof(s));        memset(vis,false,sizeof(vis));        for(i=0,cnt=0;i<n;++i){        memset(v,false,sizeof(v));            cin >> str;cnt=0;            for(j=0;str[j];++j){                tmp = str[j] - 'a';                if(!v[tmp])cnt++;             s[i][tmp]++;             s[i][26]++;             v[tmp]=true;            }            if(cnt>2)vis[i] = true;        }            for(i=0;i<26;++i){                for(j=i+1;j<26;++j){                    c=0;                    for(int k=0;k<n;++k){                    if(jugde(i,j,k))c+=s[k][26];                    }                    ma = max(ma,c);                }                            }        printf("%d\n",ma);    }    return 0;}


1 0
原创粉丝点击