码迷,mamicode.com
首页 > 其他好文 > 详细

[USACO][枚举]Preface Numbering

时间:2020-01-23 22:48:15      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:枚举   ring   思路   tor   暴力   out   cond   罗马数字   就是   

题意:

输入一个数字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 }

[USACO][枚举]Preface Numbering

标签:枚举   ring   思路   tor   暴力   out   cond   罗马数字   就是   

原文地址:https://www.cnblogs.com/ggy778/p/12231448.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!