题意:给定一个 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; }