poj1171

来源:互联网 发布:深圳好吃的蛋糕店 知乎 编辑:程序博客网 时间:2024/06/06 03:51

//============================================================================
// Name        : poj1171.cpp
// Author      : friendy
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
//第一次在linux上到eclise里面写代码,虽然很方便但是不是很熟悉,所以这么一个水题,搞了半天
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct Word{//结构重载为了排序
    char str[8];
    int len,value;
    bool operator<(const Word &other)const{
        return len<other.len;
    }
}word[40002];
int hash[26]={0},hash1[26]={0};
int a[]={2,5,4,4,1,6,5,5,1,7,6,3,5,2,3,5,7,2,1,2,4,6,6,7,5,7};//映射表

int main() {
    int sum=0,sum1=0,mmax=0,j,i;
    char ch;

    while(scanf("%c",&ch)&&ch!='/n'){//读入第一个单词,也就是以后所有到单词到字母必须属于这个集合才求最大值
        hash1[ch-'a']++;
        sum1+=a[ch-'a'];
    }

    int num=0;
    while(gets(word[num].str)&&word[num].str[0]!='.'){//读单词
        i=-1;sum=0;
        memset(hash,0,sizeof(hash));
        while(word[num].str[++i]!='/0'){
            if(++hash[word[num].str[i]-'a']>hash1[word[num].str[i]-'a'])
                break;
            sum+=a[word[num].str[i]-'a'];
        }

        if(word[num].str[i]=='/0'){
            if(sum>mmax)
                mmax=sum;
            if(i<5){//大于5 到不用存储
                word[num].len=i;
                word[num].value=sum;
                ++num;
            }
        }
    }

    sort(word,word+num);//排序
    int len3=0,k,l;
    for(i=0;i<num;i++)
        if(word[i].len<=3)
            len3=i;
    for(i=sum1;i>0;i--){//从最大值进行枚举,找到就输出

        if(i==mmax){
            printf("%d/n",i);
            return 0;
        }
        //组合长度为3和4到字符串
        for(k=num-1;k>0;k--){
            for(j=len3;j>=0;j--){
                if(k==j)
                    continue;
                sum=0;
                memset(hash,0,sizeof(hash));
                for(l=0;l<word[k].len;l++){
                    ++hash[word[k].str[l]-'a'];
                }
                for(l=0;l<word[j].len;l++){
                    if(++hash[word[j].str[l]-'a']>hash1[word[j].str[l]-'a'])
                        break;
                }
                if(l==word[j].len&&i==word[k].value+word[j].value){
                    printf("%d/n",i);
                    return 0;
                }
            }
        }

    }
    return 0;
}

原创粉丝点击