• Codeforces Round #329 (Div. 2)A 字符串处理


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

    字符串处理的题目 体现代码能力之差

    题意:输入n 个字符串  输出 最多只包含两种字符(拼凑成的整个字符串只有两种字符)的 所有字符串长度之和

    处理:输入n个字符串 判断每个字符串种 字符的种类个数 如果 不大于3 则存下来      代码中用 jishu[][]数组存下相应字符的assii玛 mp数组记录出现次数

    然后遍历所有字符组合 两个for循环 判断每行的两个字符 或1个字符是否与 当前组合相同 相同则 加到linshi (判断该行1个字符还是两个字符看代码   另外 注意处理 当前组合为同一字符的情况 避免重复) linshi  取最大值输出!

    #include<bits/stdc++.h>
    using namespace std;
    int n;
    char  a[1000];
    int mp[105][150];
    int jishu[105][5];
    int flag;
    int main()
    {
        scanf("%d",&n);
        memset(mp,0,sizeof(mp));
        getchar();
        for(int i=1; i<=n; i++)
        {
            memset(a,0,sizeof(a));
            gets(a);
            flag=0;
            for(unsigned int j=0; j<strlen(a); j++)
            {
                if(mp[i][a[j]]==0)
                {
                    jishu[i][flag]=a[j];
                    // cout<<jishu[i][flag]<<endl;
                    flag++;
                }
                mp[i][a[j]]++;
                //cout<<mp[i][a[j]]<<endl;
                if(flag>2)
                {
                    memset(mp[i],0,sizeof(mp[i]));
                    memset(jishu[i],0,sizeof(jishu[i]));
                    break;
                }
            }
        }
        int re=0;
        int linshi=0;
        for(int j='a'; j<='z'; j++)
            for(int k='a'; k<='z'; k++)
            {
                linshi=0;
                for(int l=1; l<=n; l++)
                {
                    if((jishu[l][0]==j&&jishu[l][1]==k)||(jishu[l][0]==k&&jishu[l][1]==j))
                    {
                        linshi+=(mp[l][j]+mp[l][k]);
                        //cout<<mp[l][j]<<" "<<mp[l][k]<<endl;
                    }
                    else
                    {
                        if(jishu[l][1]<97||jishu[l][1]>123)
                        {
                            if(jishu[l][0]==j)
                            {
                                linshi+=mp[l][j];
                                //cout<<linshi<<"*"<<endl;
                            }
    
                            if(jishu[l][0]==k&&j!=k)
                            {
                                linshi+=mp[l][k];
                                //cout<<linshi<<"**"<<endl;
                            }
                        }
                    }
    
                }//cout<<j<<k<<linshi<<"**"<<endl;
                re=max(re,linshi);
    
            }
        //cout<<linshi<<endl;
        cout<<re<<endl;
        return 0;
    }
    

      

  • 相关阅读:
    grep命令
    Linux下tar.xz结尾的文件的解压方法
    const char*, char const*, char*const的区别
    "undefined reference to" 多种可能出现的问题解决方法
    Linux查找含有某字符串的所有文件
    Netbeans C++ unable to resolve identifier 无法解析标识符
    Linux 下编译C程序的全过程
    linux tar.gz zip 解压缩 压缩命令
    安装anaconda
    Mongodb数据迁移步骤
  • 原文地址:https://www.cnblogs.com/hsd-/p/4941297.html
Copyright © 2020-2023  润新知