HDU 6034 贪心
来源:互联网 发布:开淘宝网店花钱吗 编辑:程序博客网 时间:2024/06/07 00:53
题目
Balala Power!
题意
给 n 个字符串,字符串只有小写字母,然后给字母从0-25进行编号,使得所有字符串按照26进制得的值相加的和最大。字符串不能有前置0,除非该字符串只含1个字母。
求最所有字符串编码后的和的最大值,答案模 109+7 。
题解
贪心,这道题思路不难,但是写起来很烦人,算是考察编程的基本功了。
我们可以按照26进制记录每个字母的权值,然后按权值大小对字母进行排序编码。
但是字符串的长度是 [1, 105],我们没法用基本类型的变量对权值进行存储。
所以要开个二维数组进行模拟,第一维记录字母,第二维记录该字母的权值,数组每一位上的数字代表26进制中的每的一位(满26进1位)。
很尴尬的是,我交 g++ 一直 wa ,然后试了下 c++ 居然过了。。。到现在我也不知道这是什么玄虚操作。。。
#include <iostream>#include <algorithm>#include <cstring>#include <stdio.h>#include <string>#include <cmath>#include <queue>#include <set>#include <map>#include <stack>#include <bitset>#include <cstdlib>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define ll long long#define ull unsigned long long#define mem(n,v) memset(n,v,sizeof(n))#define MAX 100020#define MAXN 10005#define opnin freopen("input.txt","r",stdin)#define clsin fclose(stdin)const ll INF = 0x3f3f3f3f;const ll INFF = 0x3f3f3f3f3f3f3f3f;const double inf = 1e18;const double eps = 1e-8;const ll mod = 1000000007;/**************************************************************************/int letters[30][MAX];int first[30];int c[30];string str[MAX];int mxLen = 0;bool cmp(int x, int y){ for (int i = mxLen-1; i >= 0; --i) { if(letters[x][i] != letters[y][i]) return letters[x][i] < letters[y][i]; } return true;}ll m[30];int main(){ int n; int t = 1; while(cin >> n){ mxLen = 0; mem(letters,0); mem(first,0); for (int i = 0; i < n; ++i) { cin >> str[i]; int len = (int)str[i].length(); mxLen = max(mxLen,len); if(len > 1) first[str[i][0] - 'a'] = 1; for (int j = 0; j < len; ++j) { letters[str[i][j] - 'a'][len - j - 1]++; } } for(int i=0; i<26; ++i){ for(int j=0; j < mxLen - 1; ++j){ letters[i][j + 1] += letters[i][j] / 26; letters[i][j] %= 26; } c[i] = i; } sort(c,c+26,cmp); int cnt = 0; while(cnt < 26 && first[c[cnt]] == 1) cnt++; int key = c[cnt]; for(int i=cnt-1;i>=0;--i){ c[i+1] = c[i]; } c[0] = key; for (int i = 0; i < 26; ++i) { m[c[i]] = i; } ll sum = 0; for(int i=0;i<n;++i){ int len = str[i].length(); ll temp = 1; for(int j=len-1;j>=0;--j){ sum = (sum + temp * m[str[i][j] - 'a'] % mod) % mod; temp = (temp * 26) % mod; } } printf("Case #%d: %lld\n",t++,sum); } return 0;}
阅读全文
0 0
- hdu 6034(贪心)
- HDU 6034 贪心
- hdu 6034 Balala Power!(贪心)
- HDU 6034 Balala Power!【贪心】
- HDU 6034 Balala Power!【贪心】
- HDU 贪心
- 【贪心】hdu
- HDU 6034 Balala Power! 排序 贪心
- HDU 6034 Balala Power! (贪心)
- HDU 6034 Balala Power!(贪心)
- HDU 6034 Balala Power!(贪心)
- HDU 6034 Balala Power!(贪心)
- HDU 6034 字符贪心赋值计算问题
- HDU 6034 Balala Power!(贪心)
- HDU 2111 Saving HDU(贪心)
- HDU 2111贪心--【Saving HDU】
- HDU 2111 Saving HDU (贪心)
- HDU Saving HDU (贪心)
- Redis各个数据类型的使用场景
- 软件框架之imageloader的使用
- 模拟退火算法基本思想
- DataFrame删除行和列
- Linux平台总线驱动设备模型
- HDU 6034 贪心
- 小程序脚本语言WXS,你想要的都在这里了
- java试题整理1(附部分个人理解)
- dp中滚动数组的应用:01背包、POJ 1159 Palindrome、多段图路径问题
- Cocos2d-x 添加TTF字体
- 安装软件需注意
- mac上xattr命令
- python 中的pickle数据序列化模块
- C++的double与%的数据类型