• 模拟字典序排序——hdu6034


    #include <bits/stdc++.h>
    #include <iostream>
    using namespace std;
    const int MAXN = 1e5+7;
    const long long mod = 1e9+7;
    int n;
    int maxj;
    struct node
    {
        int id;
        int num[MAXN]={0};
        bool operator < (const node &a)const
        {
            for(int j = maxj; j >= 0; --j)
                if(num[j] != a.num[j])
                    return num[j] > a.num[j];
            return 0;
        }
    } p[30];
    string s[MAXN];
    long long num[30];
    long long k[MAXN];
    int book[30];
    int main()
    {
        ios::sync_with_stdio(false);
        k[0] = 1;
        for(int i = 1 ; i <= 100000; ++i)k[i] = (k[i-1]*26)%mod;
     
        int ca = 0;
        maxj=0;
        while(cin>>n)
        {
     
            for(int i = 0; i < 26; ++i)
            {
                book[i]=0;
                for(int j=0; j<=maxj; j++)p[i].num[j]=0;
                p[i].id = i;
            }
     
            for(int i = 0; i < n; ++i)
            {
                cin>>s[i];
                int l = s[i].size();
                for(int ii = l-1,j=0; ii >=0; --ii,++j)
                {
                    if(ii==0)book[s[i][ii]-'a']=1;
                    int t = s[i][ii]-'a';
                    p[t].num[j]++;
                }
            }
     
            maxj=0;
            for(int i = 0; i < 26; ++i)
            {
                for(int j = 0; j <= 100000; ++j)
                {
                    p[i].num[j+1] += p[i].num[j]/26;
                    p[i].num[j]%=26;
                    if(p[i].num[j]>0)maxj=max(j, maxj);
                }
            }
            sort(p,p+26);
            
            int i;
            if(book[p[25].id])
            {
                for(i=25; i>=0; i--)if(book[p[i].id]==0)break;
                p[26]=p[i];
                for(; i<26; i++)p[i]=p[i+1];
            }
            for(int i = 25; i >= 0; --i)num[p[25-i].id] = i;
            
            //printf("%d
    ",p[0].id);
            long long ans = 0;
            for(int i = 0; i < n; ++i)
            {
                int l = s[i].size();
                for(int j = 0; j < l; ++j)
                {
                    //printf("%d
    ", num[s[i][j]-'a']);
                    ans = (ans+num[s[i][j] -'a']*k[l-1-j]%mod)%mod;
                }
            }
            cout<<"Case #"<<++ca<<": "<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    iOS开源控件库收集
    Ruby中的几种除法
    Font
    PlaySound
    STL
    APIs
    cin and cout
    CreateWindow
    Introducing Direct2D
    VC 常用代码
  • 原文地址:https://www.cnblogs.com/zsben991126/p/11186758.html
Copyright © 2020-2023  润新知