HUAS_ACM 个人训练#4

                                                                                           A

题目连接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=117542#problem/A

题意:给出n个单词(字符串),选出最长的字符串,要求选出的字符串中的字符不超过2种,输出满足条件

的字符串的长度。

分析:因为只有26个字符,所以满足条件的字符串中含有字符有26*26种的可能,找出这26*26种中最长的即可。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn=105;
    char str[maxn][maxn];
int main()
{
    int m;
     cin>>m;
        for(int i=0; i<m; i++)
            cin>>str[i];
        int Max = 0;
        for(char i='a'; i<='z'; i++)
        {
            for(char j='a'; j<='z'; j++)
            {
                int ret = 0;
                for(int x=0;x<m;x++)
                {
                    int c=1;
                    for(int y=0;y<strlen(str[x]);y++)
                       if(str[x][y]!=i&&str[x][y]!=j) //除掉出现第三种字符的单词
                                 c=0;                 
                if(c==1)
                    ret=ret+strlen(str[x]);
                }
                Max = max(Max, ret);
            }
        }
        cout<<Max<<endl;
return 0;
}
原文地址:https://www.cnblogs.com/fenhong/p/5539886.html