zoukankan      html  css  js  c++  java
  • [USACO][枚举]Preface Numbering

    题意:

    输入一个数字N,输出[1,N]中数字转化为罗马数字之后每个字母出现的次数。

    思路:

    暴力过的...写了一个阿拉伯数字转换罗马数字的程序,然后枚举数字string找的字母。

    遇到的坑就是罗马数字没有450的简短表示!!

    leetcode上面有罗马数字和阿拉伯数字互相准换的题目,可能是受这个影响吧......

    Analysis的题解里面有这些值得学习的地方:
    * strcat把两个char[]连接起来

    剩下的题解没详细地去看懂><

     1 /*
     2 ID :ggy_7781
     3 TASK :prefix
     4 LANG :C++11
     5 */
     6 
     7 #include <bits/stdc++.h>
     8 using namespace std;
     9 
    10 char a[200003];
    11 char strs[203][13];
    12 char tmp[79];
    13 bool cmp(pair<int,int> l,pair<int,int>r)
    14 {
    15     if(l.first != r.first)
    16         return l.first < r.first;
    17     return l.second < r.second;
    18 }
    19 int main(){
    20     freopen("prefix.in","r",stdin);
    21     freopen("prefix.out","w",stdout);
    22     int tot =0;
    23     while(cin>>strs[tot])
    24     {
    25         if(strs[tot][0] == '.')
    26             break;
    27         tot ++;
    28     }
    29     while(cin>>tmp)
    30         strcat(a,tmp);
    31     int len = strlen(a);
    32     vector<pair<int,int> > ans;
    33     for(int i = 0;i < tot;i ++)
    34     {
    35         int leni = strlen(strs[i]);
    36         for(int j = 0;j < len;j ++)
    37         {
    38             if(a[j] == strs[i][0])
    39             {
    40                 int op = j;
    41                 bool ok = true;
    42                 int k;
    43                 for(k = 0;k < leni && j <len;k ++,j++)
    44                 {
    45                     if(strs[i][k]!=a[j])
    46                     {
    47                         ok = false;
    48                         break;
    49                     }
    50                 }
    51                 if(ok && k==leni)
    52                 {
    53                     ans.push_back(pair<int,int>(op,j) );
    54                 }
    55                 j = op;
    56             }
    57         }
    58     }
    59     sort(ans.begin(),ans.end(),cmp);
    60     if(ans[0].first != 0)
    61     {
    62         cout<<0<<endl;
    63         return 0;
    64     }
    65     int nowpos = ans[0].second;
    66     int j = 0;
    67     while(j < ans.size() && ans[j].first <= nowpos && nowpos <= ans[j].second)
    68     {
    69         nowpos = ans[j].second;
    70         j ++;
    71     }
    72     cout<<nowpos<<endl;
    73     return 0;
    74 }
  • 相关阅读:
    Java知识15 Number&Math类【多测师】
    python callable()方法实例
    高级super实例
    高级any、for组合用法
    python 字典update、setdefault、pop方法案例
    一个经典的python字典生成式案例
    一个发挥到极致的yield案例
    python map使用
    Python yield详解
    django __path__使用
  • 原文地址:https://www.cnblogs.com/ggy778/p/12231448.html
Copyright © 2011-2022 走看看