hihocoder #1566 : 皇室成员的名字

把每个罗马数字转化成 阿拉伯数字

其实每次做做最长匹配就行

然后sort 

#include<cstdio>
#include<cstring>
#include<map>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<map>
#include<queue>
#include<set>
#include<vector>
using namespace std;


#define inf  1000000007
#define ll long long
#define MAXN   500010

string s[35]={"I","II","III","IV","V","VI","VII","VIII","IX","X","XX","XXX","XL","L","LX","LXX","LXXX","XC","C","CC","CCC","CD","D","DC","DCC","DCCC","CM","M","MM","MMM"};
int  z[35]={     1,  2,    3,    4,  5,   6,   7,     8,    9, 10,  20  ,30,   40,  50 ,60,  70,    80,   90, 100, 200 ,300, 400 ,500, 600 ,700 , 800,  900 ,1000,2000,3000 };


struct node
{
    string a,c;
    int b;
}y[MAXN];

int calc(string b)
{
    int len=b.size();
    int sum=0;
    for(int i=0;i<len;)
    {
        int k=i;
        string a="";
        int mx=0;

        while(k<len)
        {
            a+=b[k];
            int ind=-1;
            for(int j=0;j<30;j++)
            {
                if(a==s[j])
                {
                    ind=j;
                    break;
                }
            }
            if(ind==-1)
            {
                break;
            }
            else
                mx=z[ind];
            k++;
        }
        sum+=mx;
        i=k;
 //     cout<<i<<" "<<mx<<endl;
    }
    return sum;
}

bool cmp(node a,node b)
{
    if(a.a==b.a)
        return a.b<b.b;
    return (a.a<b.a);
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
        {
            string b;
            cin>>y[i].a>>b;
            y[i].c=b;
            y[i].b=calc(b);
        }
        sort(y+1,y+n+1,cmp);
        for(int i=1;i<=n;i++)
            cout<<y[i].a<<" "<<y[i].c<<endl;
    }
    return 0;
}
View Code

 

posted on 2017-11-07 09:07  HelloWorld!--By-MJY  阅读(171)  评论(0编辑  收藏  举报

导航