HDU 6034 Balala Power! (贪心+坑题)
题意:给定一个 n 个字符串,然后问你怎么给 a-z赋值0-25,使得给定的字符串看成26进制得到的和最大,并且不能出现前导0.
析:一个很恶心的题目,细节有点多,首先是思路,给定个字符一个权值,然后要注意的进位,然后排序,从大到小,给每个字符赋值,如果最后一个出现前导0,就得向前找一个最小的不在首字符的来交换,但不能动了相对的顺序。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 50;
const LL mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r > 0 && r <= n && c > 0 && c <= m;
} vector<string> v;
int val[30];
set<int> sets; struct Node{
int v[maxn];
int id;
int len;
bool operator < (const Node &p) const{
int ma = max(len, p.len);
for(int i = ma-1; i >= 0; --i){
if(v[i] != p.v[i]) return v[i] > p.v[i];
}
return false;
}
};
Node a[27]; int main(){
ios::sync_with_stdio(false);
int kase = 0;
while(cin >> n){
string s;
memset(a, 0, sizeof a);
for(int i = 0; i < 26; ++i) a[i].id = i; v.clear();
sets.clear();
for(int i = 0; i < n; ++i){
cin >> s;
v.push_back(s);
int ttt = (int)s.size()-1;
if(s.size() > 1) sets.insert(s[0] - 'a');
for(int j = 0; j < s.size(); ++j, --ttt){
++a[s[j]-'a'].v[ttt];
a[s[j]-'a'].len = max(a[s[j]-'a'].len, ttt+1);
}
}
for(int i = 0; i < 26; ++i){
for(int j = 0; j < a[i].len; ++j){
a[i].v[j+1] += a[i].v[j] / 26;
a[i].v[j] %= 26;
}
while(a[i].v[a[i].len] >= 26){
a[i].v[a[i].len+1] += a[i].v[a[i].len] / 26;
a[i].v[a[i].len] %= 26;
++a[i].len;
}
if(a[i].v[a[i].len] > 0 && a[i].v[a[i].len] < 26) ++a[i].len;
} sort(a, a + 26);
LL ans = 0;
int tt = 25;
memset(val, 0, sizeof val);
for(int i = 0; i < 26; ++i){
if(a[i].len == 0) continue;
val[a[i].id] = tt;
--tt;
} if(tt < 0 && sets.count(a[25].id)){
int t = 25;
for(int j = 24; j >= 0; --j){
if(!sets.count(a[j].id)){ swap(val[a[t].id], val[a[j].id]); break; }
else { swap(val[a[t].id], val[a[j].id]); t = j; }
}
} for(int i = 0; i < v.size(); ++i){
LL tmp = 0;
for(int j = 0; j < v[i].size(); ++j)
tmp = (tmp * 26 + val[v[i][j]-'a']) % mod;
ans = (ans + tmp) % mod;
}
cout << "Case #" << ++kase << ": " << ans << endl;
}
return 0;
}
HDU 6034 Balala Power! (贪心+坑题)的更多相关文章
- HDU 6034 - Balala Power! | 2017 Multi-University Training Contest 1
/* HDU 6034 - Balala Power! [ 大数进位,贪心 ] 题意: 给一组字符串(小写英文字母),将上面的字符串考虑成26进制数,每个字母分配一个权值,问这组数字加起来的和最大是多 ...
- 2017 Multi-University Training Contest - Team 1 1002&&HDU 6034 Balala Power!【字符串,贪心+排序】
Balala Power! Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- HDU 6034 Balala Power!(贪心+排序)
Balala Power! Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- hdu 6034 Balala Power!
Balala Power! Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- 2017ACM暑期多校联合训练 - Team 1 1002 HDU 6034 Balala Power! (字符串处理)
题目链接 Problem Description Talented Mr.Tang has n strings consisting of only lower case characters. He ...
- HDU 6034 Balala Power!【排序/进制思维】
Balala Power![排序/进制思维] Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java ...
- HDU 6034 Balala Power! —— Multi-University Training 1
Talented Mr.Tang has nn strings consisting of only lower case characters. He wants to charge them wi ...
- hdu 6034 B - Balala Power! 贪心
B - Balala Power! 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=6034 题面描述 Talented Mr.Tang has n st ...
- HDU 2985 Another lottery(坑题)
点我看题目 题意 : 有n个人,每个人可以玩m轮,每一轮中每个参与者都有cj张票,第 i 轮的奖金是2的i次方,问你每个人所能赢得最多的奖金的概率是多少. 思路 : 这个题比较坑啊,其实不用去算前几轮 ...
随机推荐
- The type org.springframework.context.ConfigurableApplicationContext cannot be resolved问题解决
在搭建maven项目的时候,有时候会报这样的问题. The type org.springframework.context.ConfigurableApplicationContext cannot ...
- 随时查找中位数——pat1057
http://pat.zju.edu.cn/contests/pat-a-practise/1057 题目的意思是可以在一个可以任意添加于删除整数的集合里随时查找该集合的中位数 每次查找用nlogn的 ...
- composer.phar的作用和安装laravel5.5.4 和 vendor目录
composer.phar有什么作用 是 PHP 用来管理依赖(dependency)关系的工具.你可以在自己的项目中声明所依赖的外部工具库(libraries),Composer 会帮你安装这些依赖 ...
- linux 下SPI通信注意事项(待续)
一.2台Linux设备之间使用SPI通信 1.标准Linux只支持Master 模式.但是可以在驱动中修改为Slave模式: 2.硬件SPI可能支持Slave模式,也可能不支持.这个要提前确认好: 3 ...
- PHP 字符串 加*
PHP字符串加* 思路: 获取第一个字符或文字 获取最后一个字符或文字 一头一尾,中间加* 尝试用substr方法,发现对中文的支持有问题. 后来发现mb_substr很好的解决了这个问题. < ...
- AD9如何设置原点位置
Edit --> Origin --> Set
- 匿名类型与Select方法实现自定义对象插入局部表结构中
在提取局部表结构数据时,通过Select选取需要的字段,如下句,此时其实产生了一个不用于_menuMan的原新数据类型new { c.SYS_COMMANDS_ID,c.TXT_COMMANDTITL ...
- 使用ReentrantReadWriteLock类
读读共享 类ReentrantReadWriteLock的使用:写写互斥 读写互斥
- IIS监控应用程序池和站点假死,自动重启IIS小工具
文章技术适合初学者.高级的C#开发工程师这些估计都熟悉到烂了,望不要喷. 第一.C#代码要操作IIS 就必须先导入 Microsoft.Web.Administration.dll ,方便控制台程序做 ...
- 英语单词state和status的区别
state:比较常用,各种状态都可以用它,但是它更着重于一种心理状态或者物理状态. Status:用在人的身上一般是其身份和地位,作"状态,情形"讲时,多指政治和商业. state ...