• STL入门练习


     

     

    看病要排队

     

    Problem Description

    看病要排队这个是地球人都知道的常识。 
    不过经过细心的0068的观察,他发现了医院里排队还是有讲究的。0068所去的医院有三个医生(汗,这么少)同时看病。而看病的人病情有轻重,所以不能根据简单的先来先服务的原则。所以医院对每种病情规定了10种不同的优先级。级别为10的优先权最高,级别为1的优先权最低。医生在看病时,则会在他的队伍里面选择一个优先权最高的人进行诊治。如果遇到两个优先权一样的病人的话,则选择最早来排队的病人。 

    现在就请你帮助医院模拟这个看病过程。

    Input

    输入数据包含多组测试,请处理到文件结束。 
    每组数据第一行有一个正整数N(0<N<2000)表示发生事件的数目。 
    接下来有N行分别表示发生的事件。 
    一共有两种事件: 
    1:"IN A B",表示有一个拥有优先级B的病人要求医生A诊治。(0<A<=3,0<B<=10) 
    2:"OUT A",表示医生A进行了一次诊治,诊治完毕后,病人出院。(0<A<=3)

    Output

    对于每个"OUT A"事件,请在一行里面输出被诊治人的编号ID。如果该事件时无病人需要诊治,则输出"EMPTY"。 
    诊治人的编号ID的定义为:在一组测试中,"IN A B"事件发生第K次时,进来的病人ID即为K。从1开始编号。 

    Sample Input

    7
    IN 1 1
    IN 1 2
    OUT 1
    OUT 2
    IN 2 1
    OUT 2
    OUT 1
    2
    IN 1 1
    OUT 1

    Sample Output

    2
    EMPTY
    3
    1
    1

    按医生的编号用优先队列数组就行

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <algorithm>
     4 #include <queue>
     5 
     6 using namespace std;
     7 
     8 struct person{
     9     int num;
    10     int pri;
    11     
    12 };
    13 
    14 struct cmp{
    15     bool operator()(person a,person b){
    16         if(a.pri!=b.pri)
    17             return a.pri<b.pri;
    18         else 
    19             return a.num>b.num;
    20     }
    21 };
    22 
    23 int main()
    24 {
    25     //freopen("sample.txt","r",stdin);
    26     int n;
    27     while(~scanf("%d",&n))
    28     {
    29         int num=0;
    30         priority_queue<person,vector<person>,cmp > qe[4];
    31         char str[5];
    32         while(n--)
    33         {
    34             scanf("%s",str);
    35             if(strcmp(str,"IN")==0)
    36             {
    37                 person p;
    38                 int a,b;
    39                 num++;
    40                 scanf("%d %d",&a,&b);
    41                 p.num=num;
    42                 p.pri=b;
    43                 qe[a].push(p);
    44             }
    45             if(strcmp(str,"OUT")==0)
    46             {
    47                 person p;
    48                 int a;
    49                 scanf("%d",&a);
    50                 if(qe[a].empty())
    51                     printf("EMPTY
    ");
    52                 else 
    53                 {
    54                     p=qe[a].top();
    55                     qe[a].pop();
    56                     printf("%d
    ",p.num);
    57                 }
    58             }
    59         }
    60     }
    61     return 0;
    62 }

    水果

    http://acm.hdu.edu.cn/showproblem.php?pid=1263

    Problem

    夏天来了~~好开心啊,呵呵,好多好多水果~~ 
    Joe经营着一个不大的水果店.他认为生存之道就是经营最受顾客欢迎的水果.现在他想要一份水果销售情况的明细表,这样Joe就可以很容易掌握所有水果的销售情况了. 

    Input第一行正整数N(0<N<=10)表示有N组测试数据. 
    每组测试数据的第一行是一个整数M(0<M<=100),表示工有M次成功的交易.其后有M行数据,每行表示一次交易,由水果名称(小写字母组成,长度不超过80),水果产地(小写字母组成,长度不超过80)和交易的水果数目(正整数,不超过100)组成. 
    Output对于每一组测试数据,请你输出一份排版格式正确(请分析样本输出)的水果销售情况明细表.这份明细表包括所有水果的产地,名称和销售数目的信息.水果先按产地分类,产地按字母顺序排列;同一产地的水果按照名称排序,名称按字母顺序排序. 
    两组测试数据之间有一个空行.最后一组测试数据之后没有空行.

     Sample Input

    1
    5
    apple shandong 3
    pineapple guangdong 1
    sugarcane guangdong 1
    pineapple guangdong 3
    pineapple guangdong 1

    Sample Output

    guangdong
       |----pineapple(5)
       |----sugarcane(1)
    shandong
       |----apple(3)

    主要用了map的嵌套

     1 #include <stdio.h>
     2 #include <iostream>
     3 #include <string.h>
     4 #include <string>
     5 #include <algorithm>
     6 #include <map>
     7 
     8 using namespace std;
     9 
    10 int main()
    11 {
    12     //freopen("sample.txt","r",stdin);
    13     int n;
    14     cin>>n;
    15     for(int i=0;i<n;i++)
    16     {
    17         if(i!=0)
    18             cout<<endl;
    19         int m;
    20         cin>>m;
    21         map<string,map<string,int> > mp;
    22         map<string,map<string,int> >::iterator it1;
    23         map<string,int>::iterator it2;
    24         while(m--)
    25         {
    26             string st1,st2;
    27             int a;
    28             cin>>st1>>st2>>a;
    29             mp[st2][st1]+=a;            //记住这种方式
    30         } 
    31         for(it1=mp.begin();it1!=mp.end();it1++)
    32         {
    33             cout<<it1->first<<endl;
    34             for(it2=it1->second.begin();it2!=it1->second.end();it2++)
    35             {
    36                 cout<<"   |----"<<it2->first<<"("<<it2->second<<")"<<endl;
    37             }
    38         }
    39     }
    40     return 0;
    41 }

    Ugly Numbers

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=72

     Problem Description

    Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence

    1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ...

    shows the first 11 ugly numbers. By convention, 1 is included. Write a program to find and print the 1500’th ugly number.

    Input

    There is no input to this program.

    Output

    Output should consist of a single line as shown below, with ‘<number>’ replaced by the number computed.

    Sample Output

    The 1500'th ugly number is <number>.

    第一次读题的时候,以为只要可以被2,3,5除尽的就是丑数,所以先后用5,3,2对待判断数取余,最后判断是否余0就行了,但是丑数是不能有除2,3,5,之外的素数因子,是允许有合数因子的。

    从1开始,丑数的2,3,5倍也都是丑数,每求出一个丑数的倍数,用set来去重,如果set中没有,扔优先队列和set中。每次从优先队列中取出的数就是这次的丑数,判断是否是第1500个。

    是的话输出,不是的话求出它的2,3,5倍,重复下去。。。。。。。。

    记得要用 long long

     1 #include <stdio.h>
     2 #include <algorithm>
     3 #include <queue>
     4 #include <set>
     5 using namespace std;
     6 
     7 typedef long long LL;
     8 int S[3]={2,3,5};
     9 priority_queue<LL,vector<LL>, greater<LL> > q;
    10 set<LL> st;
    11 
    12 int main()
    13 {
    14     q.push(1);
    15     st.insert(1);
    16     for(int i=1;;i++)
    17     {
    18         LL t=q.top();
    19         q.pop();
    20         if(i==1500)
    21         {
    22             printf("The 1500'th ugly number is %lld.
    ",t);
    23             break;
    24         }
    25         for(int j=0;j<3;j++)
    26         {
    27             if(!st.count(t*S[j]))
    28             {
    29                 q.push(t*S[j]);
    30                 st.insert(t*S[j]);
    31             }
    32         }
    33     }
    34     return 0;
    35 }

    士兵队列训练问题

     http://acm.hdu.edu.cn/showproblem.php?pid=1276

    Problem

    某部队进行新兵队列训练,将新兵从一开始按顺序依次编号,并排成一行横队,训练的规则如下:从头开始一至二报数,凡报到二的出列,剩下的向小序号方向靠拢,再从头开始进行一至三报数,凡报到三的出列,剩下的向小序号方向靠拢,继续从头开始进行一至二报数。。。,以后从头开始轮流进行一至二报数、一至三报数直到剩下的人数不超过三人为止。 

    Input

    本题有多个测试数据组,第一行为组数N,接着为N行新兵人数,新兵人数不超过5000。 

    Output

    共有N行,分别对应输入的新兵人数,每行输出剩下的新兵最初的编号,编号之间有一个空格。 

    Sample Input

    2
    20
    40

    Sample Output

    1 7 19
    1 19 37

    这题有点坑啊,第一次没过去,主要是以为当n小于3的话,至少也要报一轮吧,就wa了。

    原来当n<=3时就不用了报数了,直接输出就好了。。。。。。。。。。。。。

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 #include <algorithm>
     5 #include <vector>
     6 
     7 using namespace std;
     8 
     9 int Flag[5001];
    10 int New[5001];
    11 int B[2]={2,3};
    12 
    13 int main()
    14 {
    15     int a;
    16     while(~scanf("%d",&a))
    17     {
    18         while(a--)
    19         {
    20             memset(Flag,0,sizeof(Flag));
    21             memset(New,0,sizeof(New));
    22             int n;
    23             scanf("%d",&n);
    24             int sum=n;
    25             for(int i=1;i<=n;i++)
    26             {
    27                 Flag[i]=1;
    28                 New[i]=i;
    29             }
    30             for(int i=0;;i++)
    31             {
    32                 if(sum<=3)
    33                 {
    34                     int cc=0;
    35                     for(int g=1;g<=n;g++)
    36                     {
    37                         if(Flag[g])
    38                         {
    39                             if(cc==0)
    40                             {
    41                                 cc++;
    42                                 printf("%d",g);
    43                             }
    44                             else 
    45                                 printf(" %d",g);    
    46                         }
    47                     }
    48                     printf("
    ");
    49                     break;
    50                 }
    51                 int count=0;
    52                 for(int j=1;j<=n;j++)
    53                 {
    54                     
    55                     if(Flag[j])
    56                     {
    57                         count++;
    58                     //    printf("i=%d j=%d count=%d 
    ",i,j,count);
    59                     }
    60                         
    61                     if(Flag[j]&&count==B[i%2])
    62                     {
    63                         sum--;
    64                         Flag[j]=0;
    65                         count=0;
    66                     }    
    67                 }
    68             }
    69         }
    70     }
    71     return 0;
    72 }

    排列2

     http://acm.hdu.edu.cn/showproblem.php?pid=1716

    Problem Description

    Ray又对数字的列产生了兴趣: 
    现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数。 

    Input

    每组数据占一行,代表四张卡片上的数字(0<=数字<=9),如果四张卡片都是0,则输入结束。 

    Output

    对每组卡片按从小到大的顺序输出所有能由这四张卡片组成的4位数,千位数字相同的在同一行,同一行中每个四位数间用空格分隔。 
    每组输出数据间空一行,最后一组数据后面没有空行。 

    Sample Input

    1 2 3 4
    1 1 2 3
    0 1 2 3
    0 0 0 0

    Sample Output

    1234 1243 1324 1342 1423 1432
    2134 2143 2314 2341 2413 2431
    3124 3142 3214 3241 3412 3421
    4123 4132 4213 4231 4312 4321
    
    1123 1132 1213 1231 1312 1321
    2113 2131 2311
    3112 3121 3211
    
    1023 1032 1203 1230 1302 1320
    2013 2031 2103 2130 2301 2310
    3012 3021 3102 3120 3201 3210

    这题不难用全排列next_permutation,主要就是格式的问题,格式弄了好久

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <iostream>
     4 #include <string>
     5 #include <algorithm>
     6 #include <set>
     7 using namespace std;
     8 
     9 int main()
    10 {
    11     //freopen("sample.txt","r",stdin);
    12     int a[4];
    13     int flag=0;
    14     while(~scanf("%d %d %d %d",&a[0],&a[1],&a[2],&a[3])&&(a[0]!=0||a[1]!=0||a[2]!=0||a[3]!=0)) 
    15     {
    16         if(flag)
    17             printf("
    ");
    18         flag=1;
    19         set<int> st;
    20         do{
    21             if(st.empty())
    22             {
    23                 if(a[0])
    24                 {
    25                     st.insert(a[0]);
    26                     printf("%d%d%d%d",a[0],a[1],a[2],a[3]);
    27                 }
    28             }
    29             else
    30             {
    31                 if(st.count(a[0]))
    32                 {
    33                     if(a[0])
    34                         printf(" %d%d%d%d",a[0],a[1],a[2],a[3]);
    35                 }
    36                 else
    37                 {
    38                     printf("
    ");
    39                     st.insert(a[0]);
    40                     if(a[0])
    41                         printf("%d%d%d%d",a[0],a[1],a[2],a[3]);    
    42                 }
    43             }
    44         }while(next_permutation(a,a+4));
    45         printf("
    ");
    46     }
    47     return 0;
    48 }

    Blue Jeans

    http://poj.org/problem?id=3080

    Description

    The Genographic Project is a research partnership between IBM and The National Geographic Society that is analyzing DNA from hundreds of thousands of contributors to map how the Earth was populated. 

    As an IBM researcher, you have been tasked with writing a program that will find commonalities amongst given snippets of DNA that can be correlated with individual survey information to identify new genetic markers. 

    A DNA base sequence is noted by listing the nitrogen bases in the order in which they are found in the molecule. There are four bases: adenine (A), thymine (T), guanine (G), and cytosine (C). A 6-base DNA sequence could be represented as TAGACC. 

    Given a set of DNA base sequences, determine the longest series of bases that occurs in all of the sequences.

    Input

    Input to this problem will begin with a line containing a single integer n indicating the number of datasets. Each dataset consists of the following components:
    • A single positive integer m (2 <= m <= 10) indicating the number of base sequences in this dataset.
    • m lines each containing a single base sequence consisting of 60 bases.

    Output

    For each dataset in the input, output the longest base subsequence common to all of the given base sequences. If the longest common subsequence is less than three bases in length, display the string "no significant commonalities" instead. If multiple subsequences of the same longest length exist, output only the subsequence that comes first in alphabetical order.

    Sample Input

    3
    2
    GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
    AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    3
    GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
    GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
    GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
    3
    CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
    ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT

    Sample Output

    no significant commonalities
    AGATAC
    CATCATCAT

    题目大意:

    给定多组样例,每组样例有n个长度为60的字符串,求这n个字符串中长度最长的公共子串,如果有多个这样的子串,按照字典序输出

    思路:可以用KMP算法解决

    从第一个主串开始查找其中的每一个子串是不是在其他子串中可以找到,如果能找到,看长度大小,如果跟最大的相同,则找字典序小的那个
    注意到,这个题给的数据范围很小,m不大于10,长度不大于60,吐过暴力枚举的话,复杂度大概是60*60*10*(60+60),不会爆,所以果断枚举

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <iostream>
     4 #include <string>
     5 #include <algorithm>
     6 
     7 using namespace std;
     8 
     9 int main()
    10 {
    11     //freopen("sample.txt","r",stdin);
    12     int n;
    13     scanf("%d",&n);
    14     while(n--)
    15     {
    16         char str[10][65];
    17         char ans[65];
    18         ans[0]=0;//别忘了,忘了就是wa 
    19         int m;
    20         scanf("%d",&m);
    21         getchar();
    22         for(int i=0;i<m;i++)
    23         {
    24             gets(str[i]);
    25         }
    26         for(int i=1;i<=strlen(str[0]);i++)
    27         {
    28             int af=0;
    29             for(int j=0;j<=strlen(str[0])-i;j++)
    30             {
    31                 int flag=1;
    32                 char ttm[65];
    33                 strncpy(ttm,str[0]+j,i);
    34                 ttm[i]=0;
    35                 for(int g=1;g<m;g++)
    36                 {
    37                     if(!strstr(str[g],ttm))
    38                     {
    39                         flag=0;
    40                         break;
    41                     }
    42                 }
    43                 if(flag)
    44                 {
    45                     af=1;
    46                     if(strlen(ttm)>strlen(ans))
    47                         strcpy(ans,ttm);
    48                     else if(strlen(ttm)==strlen(ans)&&strcmp(ttm,ans)<0)
    49                         strcpy(ans,ttm);
    50                 }
    51             }
    52             if(!af)
    53                 break;
    54         }
    55         if(strlen(ans)<3)
    56             printf("no significant commonalities
    ");
    57         else
    58             puts(ans);
    59     }
    60     return 0;
    61 }

    Fence Repair

    http://poj.org/problem?id=3253

    Description

    Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into the N planks (i.e., whose length is the sum of the lengths Li). FJ is ignoring the "kerf", the extra length lost to sawdust when a sawcut is made; you should ignore it, too.

    FJ sadly realizes that he doesn't own a saw with which to cut the wood, so he mosies over to Farmer Don's Farm with this long board and politely asks if he may borrow a saw.

    Farmer Don, a closet capitalist, doesn't lend FJ a saw but instead offers to charge Farmer John for each of the N-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.

    Farmer Don then lets Farmer John decide the order and locations to cut the plank. Help Farmer John determine the minimum amount of money he can spend to create the N planks. FJ knows that he can cut the board in various different orders which will result in different charges since the resulting intermediate planks are of different lengths.

    Input

    Line 1: One integer N, the number of planks 
    Lines 2..N+1: Each line contains a single integer describing the length of a needed plank

    Output

    Line 1: One integer: the minimum amount of money he must spend to make N-1 cuts

    Sample Input

    3
    8
    5
    8

    Sample Output

    34

    Hint

    He wants to cut a board of length 21 into pieces of lengths 8, 5, and 8. 
    The original board measures 8+5+8=21. The first cut will cost 21, and should be used to cut the board into pieces measuring 13 and 8. The second cut will cost 13, and should be used to cut the 13 into 8 and 5. This would cost 21+13=34. If the 21 was cut into 16 and 5 instead, the second cut would cost 16 for a total of 37 (which is more than 34).

    思路:堆,用优先队列就可以做

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <iostream>
     4 #include <string>
     5 #include <algorithm>
     6 #include <queue>
     7 using namespace std;
     8 
     9 typedef long long LL;
    10 
    11 int main()
    12 {
    13     int n;
    14     while(~scanf("%d",&n))
    15     {
    16         priority_queue<LL,vector<LL>,greater<LL> > qe;
    17         for(int i=0;i<n;i++)
    18         {
    19             int t;
    20             scanf("%d",&t);
    21             qe.push(t);
    22         }
    23         LL sum=0;
    24         while(qe.size()>1)
    25         {
    26             int a=qe.top();
    27             qe.pop();
    28             int b=qe.top();
    29             qe.pop();
    30             sum=sum+a+b;
    31             qe.push(a+b);
    32         }
    33         printf("%lld
    ",sum);
    34     }
    35     return 0;
    36 }
  • 相关阅读:
    Android长方形图片生成正圆形,以及矩形图片生成圆角
    android应用编译失败 ResXMLTree_node size 类错误,以及 android studio 项目内搜索
    Android 不能返回 parent Activity 的问题
    如何让有物理键的手机在ActionBar始终显示更多菜单menu键
    Android抽屉效果 DrawerLayout 入门经验总结
    Android使用Home键后应用程序重启的问题
    汉王云名片识别(SM)组件开发详解
    [deviceone开发]-购物车的简单示例
    [DeviceOne开发]-白板的示例
    [DeviceOne开发]-地区选择
  • 原文地址:https://www.cnblogs.com/jiamian/p/11174254.html
Copyright © 2020-2023  润新知