codeforces 593A 2char
2015-11-24 21:20
344 查看
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’}.
题目意思:只能用26个字母中的两个字母,在所给的n个串中找到能用两个字母组成的所有串最长总长度,所以若一个串含有三个以上字母,则抛弃掉;
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’}.
题目意思:只能用26个字母中的两个字母,在所给的n个串中找到能用两个字母组成的所有串最长总长度,所以若一个串含有三个以上字母,则抛弃掉;
#include <bits/stdc++.h> using namespace std; int main(){ ios::sync_with_stdio(false); int n; string s; int k[26][26]; //记录只由字母a和字母b组成的所有串的总长度k[a][b]; while(cin >> n){ memset(k,0,sizeof(k)); // 初始化为0 while(n--) { cin >> s; char a = '0'; //定义临时变量字符a和字符b char b = '0'; int flag = 1; a = s[0]; //先让a为s[0]; //检查一个串中是否含有3个及以上不同字符 for(int i = 1; i < s.size(); i++) { if(s[i] != a) { //检查到一个不同的 if(b != s[i]) { //看它是否等于b if(b == '0') b = s[i]; //若此时b还未被赋值,则b赋值为s[i],即串中已有两个不同的字符了 else { //若b已经被赋值,则表示此时s[i] != a 且 != b,不满足条件,此串被标记为flag = 0; flag = 0; break; } } } } if(flag) { // flag 为1的串,即为未被抛弃的串 if(b == '0') k[a-'a'][a-'a'] = k[a-'a'][a-'a'] + s.size(); //如果未被赋值,则此串只由一个字符构成更新仅由此字符构成的串的长度k[a][a]; else k[a-'a'][b-'a'] = k[b-'a'][a-'a'] = k[a-'a'][b-'a'] + s.size();//否则,更新由a,b两个字符构成的串的长度 } } // 由k[a][b] = k[a][a] + k[b][b],更新所有由两个字符字符构成的串的总长度 for(int i = 0; i < 26; i++) for(int j = 0; j < 26; j++) { if(i == j ) continue;// 相同字符构成的串则不需要啦 k[i][j] = k[i][j] + k[i][i] + k[j][j]; } int maxn = -1; //查找最大值 for(int i = 0; i < 26; i++) for(int j = 0; j < 26; j++){ if(maxn < k[i][j]) maxn = k[i][j]; } cout << maxn << endl; } return 0; }
相关文章推荐
- 数据库链接字符串查询网站
- Flex字符串比较 还有Flex字符串操作
- Ruby中创建字符串的一些技巧小结
- ASP下经常用的字符串等函数参考资料
- 将字符串小写转大写并延时输出的批处理代码
- 将字符串转换成System.Drawing.Color类型的方法
- Lua源码中字符串类型的实现
- Lua性能优化技巧(四):关于字符串
- 字符串聚合函数(去除重复值)
- Ruby中的字符串编写示例
- 总结的5个C#字符串操作方法分享
- sqlserver中求字符串中汉字的个数的sql语句
- sql server字符串非空判断实现方法
- VBS的字符串及日期操作相关函数
- C#实现将千分位字符串转换成数字的方法
- jquery 删除字符串最后一个字符的方法解析
- PowerShell实现在字符串中查找大写字母
- PowerShell中使用Out-String命令把对象转换成字符串输出的例子
- PowerShell中字符串使用单引号和双引号的区别
- Powershell小技巧之获取字符串的行数