首页 技术 正文
技术 2022年11月19日
0 收藏 655 点赞 5,028 浏览 2290 个字

这个题。。。一开始看了很久题目(并且问了机房几个大佬)才明白题意。。 (原题入口

题意
大概是一开始给你一些字母出现的次数 你之后组成的单词(最多两个单词)每个字母出现次数 必须小于或等于标准(standard)的次数
其次是要满足你组成单词的 每个字符个数 * 该字符单个价值 最大。 如果有多解按字典序来。

自己的理解:
这个题目很良心 单词表可以去USACO官网上看。。都很短 而且给你的是按字典序来的 就不需要手动排序了
再其次楼下都讲了 很多单词存都不用存 一开始判断下就行了
对于这种东西 就应该用结构体存储 并且重载一些运算符 或者 成员函数 (因为很多操作是很重复的)
代码:

 #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cctype>
#include <iostream>
#define For(i, l, r) for(int i = (l); i <= (int)(r); ++i)
#define Fordown(i, r, l) for(int i = (r); i >= (int)(l); --i)
#define Set(a, v) memset(a, v, sizeof(a))
using namespace std; inline int read(){
int x = , fh = ; char ch;
for(; !isdigit(ch); ch = getchar()) if (ch == '-') fh = -;
for(; isdigit(ch); ch = getchar()) x = (x<<) + (x<<) + (ch^'');
return x * fh;
} const int N = ;
const int score[]={, , , , , , , , , , , , , , , , , , , , , , , , , };
//当然要把这个表打出来了。。但我太懒了。。copy了一下redbag大神的。。 struct node {
char str[];
int dig[]; //存储了每个单词出现的次数 void update () {
int n = strlen(str);
Set(dig, );
For (i, , n-)
dig[str[i] - 'a'] ++;
} //更新每个字符出现的次数 int calc () {
int ans = ;
For (i, , )
ans += dig[i] * score[i];
return ans;
} //计算这个单词的得分 bool operator > (const node &rhs) const {
For (i, , )
if (dig[i] > rhs.dig[i]) return true;
return false;
} //比较单词每个字母出现次数多少 如果多的话 则不满足题目要求 node operator + (const node &rhs) const {
node ans;
Set(ans.dig, );
For (i, , )
ans.dig[i] = dig[i] + rhs.dig[i];
return ans;
} //把两个单词出现次数加起来
}; node standard, word[N];
int cnt = ;
struct ans_pair {
int word1, word2; //答案的词对 如果只有一个单词则第二个为0 这个存储的是单词的编号
}; #include <vector>
vector <ans_pair> lt; //听说 不定长数组 和 答案序列 更配哦 (滑稽) int ans_num = ;
int main(){
scanf ("%s", standard.str);
standard.update(); //把初始的标准更新
while (scanf ("%s", word[cnt].str) != EOF && word[cnt].str[] != '.') {
word[cnt].update();
if (word[cnt] > standard) continue; //如果不满足要求就不能存储 让下一个输入的覆盖掉
++cnt;
}
--cnt; //去掉最后一个'.'
For (i, , cnt) {
int now_score = ; //当前成绩
now_score = word[i].calc();
if (now_score > ans_num)
{ans_num = now_score; lt.clear(); lt.push_back((ans_pair) {i, } );} //比原来答案大 就更新一下
else if (now_score == ans_num) lt.push_back((ans_pair) {i, } ); //相同就放入vector For (j, i+, cnt) {
node new_word = word[i] + word[j]; //计算合并后单词的dig
if (new_word > standard ) continue; //不满足要求就继续找
now_score = new_word.calc(); //计算成绩
if (now_score > ans_num) //同上
{ans_num = now_score; lt.clear(); lt.push_back((ans_pair) {i, j} );}
else if (now_score == ans_num) lt.push_back((ans_pair) {i, j} );
}
} printf ("%d\n", ans_num);
For (i, , lt.size()-) {
int fir = lt[i].word1, sec = lt[i].word2; //first 第一个单词 second 第二个单词
if (sec) printf ("%s %s\n", word[fir].str, word[sec].str);
else printf ("%s\n", word[fir].str); //如果只有一个单词 输出第一个单词就好了
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,028
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,518
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,367
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,146
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,858