codeforces #329 div 2 A. 2Char (暴力)

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.

Sample test(s)

input

4<br></br>abb<br></br>cacc<br></br>aaa<br></br>bbb

output

9

input

5<br></br>a<br></br>a<br></br>bcbcb<br></br>cdecdecdecdecdecde<br></br>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'}.

昨天真是智商掉线。

首先对于单词中出现字母超过三个的肯定要排除掉,因为单词并不能分割。

然后用cnt1[]和cnt2[][]两个数组,分别统计当只有一个字母或者两个字母的时候,所有有该字母(或两个字母)的单词,对答案的贡献度(就是单词的长度)

<span style="color: #008080;">  1</span> <span style="color: #008000;">/*</span><span style="color: #008000;">************************************************************************
</span><span style="color: #008080;">  2</span> <span style="color: #008000;">    > File Name: code/cf/#329/A.cpp
</span><span style="color: #008080;">  3</span> <span style="color: #008000;">    > Author: 111qqz
</span><span style="color: #008080;">  4</span> <span style="color: #008000;">    > Email: rkz2013@126.com 
</span><span style="color: #008080;">  5</span> <span style="color: #008000;">    > Created Time: 2015年11月05日 星期四 15时02分04秒
</span><span style="color: #008080;">  6</span> <span style="color: #008000;"> ***********************************************************************</span><span style="color: #008000;">*/</span>
<span style="color: #008080;">  7</span> 
<span style="color: #008080;">  8</span> #include<iostream>
<span style="color: #008080;">  9</span> #include<iomanip>
<span style="color: #008080;"> 10</span> #include<cstdio>
<span style="color: #008080;"> 11</span> #include<algorithm>
<span style="color: #008080;"> 12</span> #include<cmath>
<span style="color: #008080;"> 13</span> #include<cstring>
<span style="color: #008080;"> 14</span> #include<<span style="color: #0000ff;">string</span>>
<span style="color: #008080;"> 15</span> #include<map>
<span style="color: #008080;"> 16</span> #include<<span style="color: #0000ff;">set</span>>
<span style="color: #008080;"> 17</span> #include<queue>
<span style="color: #008080;"> 18</span> #include<vector>
<span style="color: #008080;"> 19</span> #include<stack>
<span style="color: #008080;"> 20</span> #include<cctype>
<span style="color: #008080;"> 21</span>                  
<span style="color: #008080;"> 22</span> <span style="color: #0000ff;">#define</span> lson l,m,rt<<1
<span style="color: #008080;"> 23</span> <span style="color: #0000ff;">#define</span> rson m+1,r,rt<<1|1
<span style="color: #008080;"> 24</span> <span style="color: #0000ff;">#define</span> ms(a,x) memset(a,x,sizeof(a))
<span style="color: #008080;"> 25</span> <span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span><span style="color: #000000;"> std;
</span><span style="color: #008080;"> 26</span> <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> dx4[<span style="color: #800080;">4</span>]={<span style="color: #800080;">1</span>,<span style="color: #800080;">0</span>,<span style="color: #800080;">0</span>,-<span style="color: #800080;">1</span><span style="color: #000000;">};
</span><span style="color: #008080;"> 27</span> <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> dy4[<span style="color: #800080;">4</span>]={<span style="color: #800080;">0</span>,-<span style="color: #800080;">1</span>,<span style="color: #800080;">1</span>,<span style="color: #800080;">0</span><span style="color: #000000;">};
</span><span style="color: #008080;"> 28</span> typedef <span style="color: #0000ff;">long</span> <span style="color: #0000ff;">long</span><span style="color: #000000;"> LL;
</span><span style="color: #008080;"> 29</span> typedef <span style="color: #0000ff;">double</span><span style="color: #000000;"> DB;
</span><span style="color: #008080;"> 30</span> <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> inf = <span style="color: #800080;">0x3f3f3f3f</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 31</span> <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> N=<span style="color: #800080;">105</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 32</span> <span style="color: #0000ff;">string</span><span style="color: #000000;"> st[N];
</span><span style="color: #008080;"> 33</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> n;
</span><span style="color: #008080;"> 34</span> <span style="color: #0000ff;">int</span> cnt1[<span style="color: #800080;">30</span>],cnt2[<span style="color: #800080;">30</span>][<span style="color: #800080;">30</span><span style="color: #000000;">];
</span><span style="color: #008080;"> 35</span> <span style="color: #0000ff;">int</span> num[<span style="color: #800080;">30</span><span style="color: #000000;">];
</span><span style="color: #008080;"> 36</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> main()
</span><span style="color: #008080;"> 37</span> <span style="color: #000000;">{
</span><span style="color: #008080;"> 38</span> <span style="color: #000000;">  #ifndef  ONLINE_JUDGE 
</span><span style="color: #008080;"> 39</span>    freopen(<span style="color: #800000;">"</span><span style="color: #800000;">in.txt</span><span style="color: #800000;">"</span>,<span style="color: #800000;">"</span><span style="color: #800000;">r</span><span style="color: #800000;">"</span><span style="color: #000000;">,stdin);
</span><span style="color: #008080;"> 40</span>   <span style="color: #0000ff;">#endif</span>
<span style="color: #008080;"> 41</span> 
<span style="color: #008080;"> 42</span>    ms(cnt1,<span style="color: #800080;">0</span><span style="color: #000000;">);
</span><span style="color: #008080;"> 43</span>    ms(cnt2,<span style="color: #800080;">0</span><span style="color: #000000;">);
</span><span style="color: #008080;"> 44</span>    ms(num,<span style="color: #800080;">0</span><span style="color: #000000;">);
</span><span style="color: #008080;"> 45</span> 
<span style="color: #008080;"> 46</span>    scanf(<span style="color: #800000;">"</span><span style="color: #800000;">%d</span><span style="color: #800000;">"</span>,&<span style="color: #000000;">n);
</span><span style="color: #008080;"> 47</span>    <span style="color: #0000ff;">for</span> ( <span style="color: #0000ff;">int</span> i = <span style="color: #800080;">0</span>  ; i < n ; i++<span style="color: #000000;">)
</span><span style="color: #008080;"> 48</span> <span style="color: #000000;">    {
</span><span style="color: #008080;"> 49</span>     cin>><span style="color: #000000;">st[i];
</span><span style="color: #008080;"> 50</span>     <span style="color: #0000ff;">int</span> len =<span style="color: #000000;"> st[i].length();
</span><span style="color: #008080;"> 51</span>     ms(num,<span style="color: #800080;">0</span><span style="color: #000000;">);
</span><span style="color: #008080;"> 52</span>     <span style="color: #0000ff;">for</span> ( <span style="color: #0000ff;">int</span> j =  <span style="color: #800080;">0</span> ;  j < len ; j++<span style="color: #000000;"> )
</span><span style="color: #008080;"> 53</span> <span style="color: #000000;">    {
</span><span style="color: #008080;"> 54</span>         <span style="color: #0000ff;">int</span> tmp =st[i][j]-<span style="color: #800000;">'</span><span style="color: #800000;">a</span><span style="color: #800000;">'</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 55</span>         num[tmp]++<span style="color: #000000;">;
</span><span style="color: #008080;"> 56</span> <span style="color: #000000;">    }
</span><span style="color: #008080;"> 57</span>     <span style="color: #0000ff;">int</span> sum=<span style="color: #800080;">0</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 58</span>     <span style="color: #0000ff;">for</span> ( <span style="color: #0000ff;">int</span> j = <span style="color: #800080;">0</span> ; j < <span style="color: #800080;">26</span> ; j++<span style="color: #000000;">)
</span><span style="color: #008080;"> 59</span> <span style="color: #000000;">    {
</span><span style="color: #008080;"> 60</span>         <span style="color: #0000ff;">if</span> (num[j]) sum++<span style="color: #000000;">;
</span><span style="color: #008080;"> 61</span> <span style="color: #000000;">    }
</span><span style="color: #008080;"> 62</span>     <span style="color: #0000ff;">if</span> (sum><span style="color: #800080;">2</span>) <span style="color: #0000ff;">continue</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 63</span>     <span style="color: #0000ff;">if</span> (sum==<span style="color: #800080;">1</span><span style="color: #000000;">)
</span><span style="color: #008080;"> 64</span> <span style="color: #000000;">    {
</span><span style="color: #008080;"> 65</span>         <span style="color: #0000ff;">int</span> x1 = st[i][<span style="color: #800080;">0</span>]-<span style="color: #800000;">'</span><span style="color: #800000;">a</span><span style="color: #800000;">'</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 66</span>         cnt1[x1] +=<span style="color: #000000;">len;
</span><span style="color: #008080;"> 67</span> <span style="color: #000000;">    }
</span><span style="color: #008080;"> 68</span>     <span style="color: #0000ff;">if</span> (sum==<span style="color: #800080;">2</span><span style="color: #000000;">)
</span><span style="color: #008080;"> 69</span> <span style="color: #000000;">    {
</span><span style="color: #008080;"> 70</span>         <span style="color: #0000ff;">int</span> x1 = st[i][<span style="color: #800080;">0</span>]-<span style="color: #800000;">'</span><span style="color: #800000;">a</span><span style="color: #800000;">'</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 71</span>         <span style="color: #0000ff;">int</span><span style="color: #000000;"> x2;
</span><span style="color: #008080;"> 72</span>         <span style="color: #0000ff;">for</span> ( <span style="color: #0000ff;">int</span> j = <span style="color: #800080;">1</span> ; j < len ; j++<span style="color: #000000;">)
</span><span style="color: #008080;"> 73</span> <span style="color: #000000;">        {
</span><span style="color: #008080;"> 74</span>         <span style="color: #0000ff;">int</span> tmp = st[i][j]-<span style="color: #800000;">'</span><span style="color: #800000;">a</span><span style="color: #800000;">'</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 75</span>         <span style="color: #0000ff;">if</span> (tmp!=<span style="color: #000000;">x1)
</span><span style="color: #008080;"> 76</span> <span style="color: #000000;">        {
</span><span style="color: #008080;"> 77</span>             x2 =<span style="color: #000000;"> tmp;
</span><span style="color: #008080;"> 78</span>             <span style="color: #0000ff;">break</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 79</span> <span style="color: #000000;">        }
</span><span style="color: #008080;"> 80</span> <span style="color: #000000;">        }
</span><span style="color: #008080;"> 81</span>         <span style="color: #0000ff;">if</span> (x1><span style="color: #000000;">x2) swap(x1,x2);
</span><span style="color: #008080;"> 82</span>         cnt2[x1][x2]+=<span style="color: #000000;">len;
</span><span style="color: #008080;"> 83</span> <span style="color: #000000;">    }
</span><span style="color: #008080;"> 84</span> <span style="color: #000000;">    }
</span><span style="color: #008080;"> 85</span>     <span style="color: #0000ff;">int</span> ans = -<span style="color: #800080;">1</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 86</span>     <span style="color: #0000ff;">int</span> cur = <span style="color: #800080;">0</span><span style="color: #000000;"> ;
</span><span style="color: #008080;"> 87</span>     <span style="color: #0000ff;">for</span> ( <span style="color: #0000ff;">int</span> i = <span style="color: #800080;">0</span> ; i < <span style="color: #800080;">25</span> ; i++<span style="color: #000000;">)
</span><span style="color: #008080;"> 88</span>     <span style="color: #0000ff;">for</span> ( <span style="color: #0000ff;">int</span> j = i+<span style="color: #800080;">1</span> ; j <<span style="color: #800080;">26</span> ; j++<span style="color: #000000;"> )
</span><span style="color: #008080;"> 89</span> <span style="color: #000000;">    {
</span><span style="color: #008080;"> 90</span>         cur = cnt1[i]+cnt1[j] +<span style="color: #000000;"> cnt2[i][j];
</span><span style="color: #008080;"> 91</span>         ans =<span style="color: #000000;"> max(ans,cur);
</span><span style="color: #008080;"> 92</span> <span style="color: #000000;">    }
</span><span style="color: #008080;"> 93</span>     printf(<span style="color: #800000;">"</span><span style="color: #800000;">%dn</span><span style="color: #800000;">"</span><span style="color: #000000;">,ans);
</span><span style="color: #008080;"> 94</span>   
<span style="color: #008080;"> 95</span>    
<span style="color: #008080;"> 96</span> <span style="color: #000000;"> #ifndef ONLINE_JUDGE  
</span><span style="color: #008080;"> 97</span> <span style="color: #000000;">  fclose(stdin);
</span><span style="color: #008080;"> 98</span>   <span style="color: #0000ff;">#endif</span>
<span style="color: #008080;"> 99</span>     <span style="color: #0000ff;">return</span> <span style="color: #800080;">0</span><span style="color: #000000;">;
</span><span style="color: #008080;">100</span> }