1. 程式人生 > >HDU 6034 Balala Power! (貪心+坑題)

HDU 6034 Balala Power! (貪心+坑題)

urn pair list ctype n+1 交換 -- val mat

題意:給定一個 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! (貪心+坑題)