Monthly Archives: October 2014

POJ 1006-Biorhythms

POJ 1006-Biorhythms
Biorhythms
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 114815 Accepted: 35991
Description
Some people believe that there are three cycles in a person's life that start the day he or she is born. These three cycles are the physical, emotional, and intellectual cycles, and they have periods of lengths 23, 28, and 33 days, respectively. There is one peak in each period of a cycle. At the peak of a cycle, a person performs at his or her best in the corresponding field (physical, emotional or mental). For example, if it is the mental curve, thought processes will be sharper and concentration will be easier.
Since the three cycles have different periods, the peaks of the three cycles generally occur at different times. We would like to determine when a triple peak occurs (the peaks of all three cycles occur in the same day) for any person. For each cycle, you will be given the number of days from the beginning of the current year at which one of its peaks (not necessarily the first) occurs. You will also be given a date expressed as the number of days from the beginning of the current year. You task is to determine the number of days from the given date to the next triple peak. The given date is not counted. For example, if the given date is 10 and the next triple peak occurs on day 12, the answer is 2, not 3. If a triple peak occurs on the given date, you should give the number of days to the next occurrence of a triple peak.
Input
You will be given a number of cases. The input for each case consists of one line of four integers p, e, i, and d. The values p, e, and i are the number of days from the beginning of the current year at which the physical, emotional, and intellectual cycles peak, respectively. The value d is the given date and may be smaller than any of p, e, or i. All values are non-negative and at most 365, and you may assume that a triple peak will occur within 21252 days of the given date. The end of input is indicated by a line in which p = e = i = d = -1.
Output
For each test case, print the case number followed by a message indicating the number of days to the next triple peak, in the form:
Case 1: the next triple peak occurs in 1234 days.
Use the plural form ``days'' even if the answer is 1.
Sample Input
0 0 0 0
0 0 0 100
5 20 34 325
4 5 6 7
283 102 23 320
203 301 203 40
-1 -1 -1 -1
Sample Output
Case 1: the next triple peak occurs in 21252 days.
Case 2: the next triple peak occurs in 21152 days.
Case 3: the next triple peak occurs in 19575 days.
Case 4: the next triple peak occurs in 16994 days.
Case 5: the next triple peak occurs in 8910 days.
Case 6: the next triple peak occurs in 10789 days.
Source
East Central North America 1999


这一题考的是中学数学,稍微分析一下:假设最终结果为rs,则距离第一天rs+d=M,也就是说在第M天,三种周期同时达到高潮,又因为三种周期某次高潮的时间为第p、e、i天,周期分别为23、28、33,所以可得M-p、M-e、M-i分别为23、28、33的倍数。又因为题干中有如下两句话:

If a triple peak occurs on the given date, you should give the number of days to the next occurrence of a triple peak.
and you may assume that a triple peak will occur within 21252 days of the given date.

所以M的取值范围为[d+1,d+21252],最后就是用穷举法来一个一个试了。具体代码如下:

#include<iostream>
#include<string>
using namespace std;
int main()
{
     int p,e,i,d;
     int case_num=1;
     while(cin>>p>>e>>i>>d&&p!=-1&&e!=-1&&i!=-1&&d!=-1)
     {
          for(int rs=d+1;rs<=21252+d;rs++)
          {
               if(((rs-p)%23==0)&&((rs-e)%28==0)&&((rs-i)%33==0))
               {
                    cout<<"Case "<<case_num<<": the next triple peak occurs in "<<rs-d<<" days."<<endl;
                    break;
               }
          }
          case_num++;
     }
     return 0;
}

提交结果AC,用时797MS,内存216K,这里还有几种优化的方法,可以参考。

hihoCoder 1039-字符消除

hihoCoder 1039-字符消除
#1039 : 字符消除
时间限制:1000ms
单点时限:1000ms
内存限制:256MB
描述
小Hi最近在玩一个字符消除游戏。给定一个只包含大写字母"ABC"的字符串s,消除过程是如下进行的:
1)如果s包含长度超过1的由相同字母组成的子串,那么这些子串会被同时消除,余下的子串拼成新的字符串。例如"ABCCBCCCAA"中"CC","CCC"和"AA"会被同时消除,余下"AB"和"B"拼成新的字符串"ABB"。
2)上述消除会反复一轮一轮进行,直到新的字符串不包含相邻的相同字符为止。例如”ABCCBCCCAA”经过一轮消除得到"ABB",再经过一轮消除得到"A"
游戏中的每一关小Hi都会面对一个字符串s。在消除开始前小Hi有机会在s中任意位置(第一个字符之前、最后一个字符之后以及相邻两个字符之间)插入任意一个字符('A','B'或者'C'),得到字符串t。t经过一系列消除后,小Hi的得分是消除掉的字符的总数。
请帮助小Hi计算要如何插入字符,才能获得最高得分。
输入
输入第一行是一个整数T(1<=T<=100),代表测试数据的数量。
之后T行每行一个由'A''B''C'组成的字符串s,长度不超过100。
输出
对于每一行输入的字符串,输出小Hi最高能得到的分数。
提示
第一组数据:在"ABCBCCCAA"的第2个字符后插入'C'得到"ABCCBCCCAA",消除后得到"A",总共消除9个字符(包括插入的'C')。
第二组数据:"AAA"插入'A'得到"AAAA",消除后得到"",总共消除4个字符。
第三组数据:无论是插入字符后得到"AABC","ABBC"还是"ABCC"都最多消除2个字符。
样例输入
3
ABCBCCCAA
AAA
ABC
样例输出
9
4
2


这个题目要稍微思考一下,当然很容易想到穷举测试,但我在边写代码的时候还是在担心穷举会不会超时什么的。后来结果还不错。
总体思路就是在字符串的所有可以插空的位置分别插入A、B、C看能消除多少个字符,然后求最大值。所以这里关键就是怎样求一个字符串最终能消除多少个字符。
题目给出的消除策略是这样的:

1)如果s包含长度超过1的由相同字母组成的子串,那么这些子串会被同时消除,余下的子串拼成新的字符串。
2)上述消除会反复一轮一轮进行,直到新的字符串不包含相邻的相同字符为止。

一定要注意,相同的子串同时消除。我一开始想用堆栈来做,来一个字符,判断是否和栈顶相同,如果相同,则消除,并统计消除个数。但是这样会有问题,比如字符串ABCCBCCCAA,如果用堆栈来做的话:进A,进B,进C,C,消除C,C,进B,消除B,B,进C,C,C,消除C,C,C,进A,A,消除A,A,A,这样所有字符都消除了。但是按原题的思路,所有相同的子串同时消除,第一轮同时只能消除C,C,C,C,C,A,A,得到ABB,然后消除B,B,最终剩A。所以用堆栈不行。
后来是这样解决的,只需遍历一遍字符串,如果s[i]==s[i+1]则说明出现重复子串了,令j=i+1,然后一个while(s[i]==s[j])循环计数。如果可以消除,则递归调用该函数继续消除,否则直接返回。
代码如下:

#include <iostream>
#include<string>
using namespace std;
//递归求解一个字符串最多能消除多少个字符
int get_erase_num(string s,int rs)
{
     int G[100]={0};//用来记录整个字符串能够消除的字符的位置
     int s_size=s.size();
     bool is_done=true;//是否检查结束的标识
     for(int i=0;i<s_size-1;i++)//注意上限s_size-1
     {
          if(s[i]==s[i+1])//然后判断是否有至少两个连续的字符出现
          {
               G[i]=1;
               is_done=false;
               rs++;
               int j=i+1;
               while(s[i]==s[j])
               {
                    G[j]=1;
                    rs++;
                    j++;
                    if(j>=s_size)//检查是否超限
                    {
                         break;
                    }
               }
               i=j-1;
          }
     }
     if(!is_done)//如果可以消除,则还没有结束
     {
          string ss="";
          for(int i=0;i<s_size;i++)
          {
               if(G[i]==0)
                    ss+=s[i];//同时消除重复子串后的结果串
          }
          return get_erase_num(ss,rs);//递归
     }
     return rs;//结束返回
}
int main()
{
     int n;
     string s;
     cin>>n;
     while(n--)
     {
          cin>>s;
          int s_size=s.size();
          int max_m=-1;
          //在所有可以插入的位置依次分别测试插入A,B,C的结果
          for(int i=0;i<s_size+1;i++)
          {
               if(i==s_size)//如果要在字符串的末尾插入字符
               {
                    int A_result=get_erase_num(s+"A",0);
                    if(A_result>max_m)
                         max_m=A_result;
                    int B_result=get_erase_num(s+"B",0);
                    if(B_result>max_m)
                         max_m=B_result;
                    int C_result=get_erase_num(s+"C",0);
                    if(C_result>max_m)
                         max_m=C_result;
               }
               else//插入位置除了末尾
               {
                    int A_result=get_erase_num(s.substr(0,i)+"A"+s.substr(i),0);
                    if(A_result>max_m)
                         max_m=A_result;
                    int B_result=get_erase_num(s.substr(0,i)+"B"+s.substr(i),0);
                    if(B_result>max_m)
                         max_m=B_result;
                    int C_result=get_erase_num(s.substr(0,i)+"C"+s.substr(i),0);
                    if(C_result>max_m)
                         max_m=C_result;
               }
          }
          cout<<max_m<<endl;
     }
     return 0;
}

该代码提交结果AC,运行用时45ms,内存0MB,还不错。

POJ 1005-I Think I Need a Houseboat

POJ 1005-I Think I Need a Houseboat
I Think I Need a Houseboat
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 87833 Accepted: 38137
Description
Fred Mapper is considering purchasing some land in Louisiana to build his house on. In the process of investigating the land, he learned that the state of Louisiana is actually shrinking by 50 square miles each year, due to erosion caused by the Mississippi River. Since Fred is hoping to live in this house the rest of his life, he needs to know if his land is going to be lost to erosion.
After doing more research, Fred has learned that the land that is being lost forms a semicircle. This semicircle is part of a circle centered at (0,0), with the line that bisects the circle being the X axis. Locations below the X axis are in the water. The semicircle has an area of 0 at the beginning of year 1. (Semicircle illustrated in the Figure.)

Input
The first line of input will be a positive integer indicating how many data sets will be included (N). Each of the next N lines will contain the X and Y Cartesian coordinates of the land Fred is considering. These will be floating point numbers measured in miles. The Y coordinate will be non-negative. (0,0) will not be given.
Output
For each data set, a single line of output should appear. This line should take the form of: “Property N: This property will begin eroding in year Z.” Where N is the data set (counting from 1), and Z is the first year (start from 1) this property will be within the semicircle AT THE END OF YEAR Z. Z must be an integer. After the last data set, this should print out “END OF OUTPUT.”
Sample Input
2
1.0 1.0
25.0 0.0
Sample Output
Property 1: This property will begin eroding in year 1.
Property 2: This property will begin eroding in year 20.
END OF OUTPUT.
Hint
1.No property will appear exactly on the semicircle boundary: it will either be inside or outside.
2.This problem will be judged automatically. Your answer must match exactly, including the capitalization, punctuation, and white-space. This includes the periods at the ends of the lines.
3.All locations are given in miles.
Source
Mid-Atlantic 2001


这题也不难,常规数学题,侵蚀面积以每年50平方米的速度进行,可以由此算出每年之后总的侵蚀面积的半径,然后通过比较给出的x、y到原点的距离判断是否在侵蚀面积之内。具体代码如下:

#include<iostream>
#include<string>
#include<cmath>
using namespace std;
double R[100];//Fred is hoping to live in this house the rest of his life.余生最多设为100年
//初始化
void init()
{
     for(int i=0;i<100;i++)
     {
          R[i]=sqrt(2*50*(i+1)/3.14);
     }
}
//二分查找
int bin_search(double r)
{
     int start=0,end=99,mid;
     while(start<=end)
     {
          mid=(start+end)/2;
          if(R[mid]>r)
               end=mid-1;
          else
               start=mid+1;
     }
     //这个时候start==end
     if(R[start]<r)
          return start+1;
     else
          return start;
}
int main()
{
     init();
     int n;
     double x,y;
     cin>>n;
     for(int i=1;i<=n;i++)
     {
          cin>>x>>y;
          double r=sqrt(x*x+y*y);
          cout<<"Property "<<i<<": This property will begin eroding in year "<<bin_search(r)+1<<"."<<endl;
     }
     cout<<"END OF OUTPUT.";
     return 0;
}

本代码提交AC,用时0MS,内存220K。

POJ 1002-487-3279

POJ 1002-487-3279
487-3279
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 245807 Accepted: 43582
Description
Businesses like to have memorable telephone numbers. One way to make a telephone number memorable is to have it spell a memorable word or phrase. For example, you can call the University of Waterloo by dialing the memorable TUT-GLOP. Sometimes only part of the number is used to spell a word. When you get back to your hotel tonight you can order a pizza from Gino’s by dialing 310-GINO. Another way to make a telephone number memorable is to group the digits in a memorable way. You could order your pizza from Pizza Hut by calling their ``three tens’’ number 3-10-10-10.
The standard form of a telephone number is seven decimal digits with a hyphen between the third and fourth digits (e.g. 888-1200). The keypad of a phone supplies the mapping of letters to numbers, as follows:
A, B, and C map to 2
D, E, and F map to 3
G, H, and I map to 4
J, K, and L map to 5
M, N, and O map to 6
P, R, and S map to 7
T, U, and V map to 8
W, X, and Y map to 9
There is no mapping for Q or Z. Hyphens are not dialed, and can be added and removed as necessary. The standard form of TUT-GLOP is 888-4567, the standard form of 310-GINO is 310-4466, and the standard form of 3-10-10-10 is 310-1010.
Two telephone numbers are equivalent if they have the same standard form. (They dial the same number.)
Your company is compiling a directory of telephone numbers from local businesses. As part of the quality control process you want to check that no two (or more) businesses in the directory have the same telephone number.
Input
The input will consist of one case. The first line of the input specifies the number of telephone numbers in the directory (up to 100,000) as a positive integer alone on the line. The remaining lines list the telephone numbers in the directory, with each number alone on a line. Each telephone number consists of a string composed of decimal digits, uppercase letters (excluding Q and Z) and hyphens. Exactly seven of the characters in the string will be digits or letters.
Output
Generate a line of output for each telephone number that appears more than once in any form. The line should give the telephone number in standard form, followed by a space, followed by the number of times the telephone number appears in the directory. Arrange the output lines by telephone number in ascending lexicographical order. If there are no duplicates in the input print the line:
No duplicates.
Sample Input
12
4873279
ITS-EASY
888-4567
3-10-10-10
888-GLOP
TUT-GLOP
967-11-11
310-GINO
F101010
888-1200
-4-8-7-3-2-7-9-
487-3279
Sample Output
310-1010 2
487-3279 4
888-4567 3
Source
East Central North America 1999


这题的思路其实很简单,使用C++的map搞定。
首先将字符串转换成标准的格式,然后将题目中给的对应关系转换成一个简单的hash数组,将字符串中的字符转换成数字(char),最后加入到map中,加入的方法使用map[s]++,如果s在map中,则s出现的次数加1;反之,则将s加入到map中,然后次数加1,即0++=1。这个方法很巧妙,是我从《C++ Primer》上看到的。
最后就是遍历map,如果s出现次数大于1,则将其输出;如果都不大于1,则输出No duplicates.
代码如下:

#include<iostream>
#include<map>
#include<string>
using namespace std;
char hash_h[26]={'2','2','2','3','3','3','4','4','4','5','5','5','6','6','6','7','0','7','7','8','8','8','9','9','9','0'};//题目给出的map对应关系,其中q和z对应0
//将输入转换成标准格式
string get_standard_form(string s)
{
     string rs="";
     int s_size=s.size();
     for(int i=0;i<s_size;i++)
     {
          if(s[i]!='-')//将s中的连接符都去掉
               rs+=s[i];
     }
     s_size=rs.size();
     for(int i=0;i<s_size;i++)
     {
          if(rs[i]>='A'&&rs[i]<='Z')
               rs[i]=hash_h[rs[i]-'A'];//替换
     }
     return rs.substr(0,3)+"-"+rs.substr(3);//添加连接符
}
int main()
{
     int n;
     string s;
     cin>>n;
     map<string,int> msi;
     while(n--)
     {
          cin>>s;
          msi[get_standard_form(s)]++;//使用map存储
     }
     map<string,int>::iterator it=msi.begin();//充分利用map的特性
     int count=0;
     while(it!=msi.end())//map已经自动按字典序排好了
     {
          if(it->second>1)
          {
               cout<<it->first<<" "<<it->second<<endl;
               count++;
          }
          it++;
     }
     if(count==0)//记得判断是否有重复的!
          cout<<"No duplicates. "<<endl;
     return 0;
}

本代码提交AC,用时1594MS,内存5160K。

POJ 1004-Financial Management

POJ 1004-Financial Management
Financial Management
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 138422 Accepted: 57882
Description
Larry graduated this year and finally has a job. He's making a lot of money, but somehow never seems to have enough. Larry has decided that he needs to grab hold of his financial portfolio and solve his financing problems. The first step is to figure out what's been going on with his money. Larry has his bank account statements and wants to see how much money he has. Help Larry by writing a program to take his closing balance from each of the past twelve months and calculate his average account balance.
Input
The input will be twelve lines. Each line will contain the closing balance of his bank account for a particular month. Each number will be positive and displayed to the penny. No dollar sign will be included.
Output
The output will be a single number, the average (mean) of the closing balances for the twelve months. It will be rounded to the nearest penny, preceded immediately by a dollar sign, and followed by the end-of-line. There will be no other spaces or characters in the output.
Sample Input
100.00
489.12
12454.12
1234.10
823.05
109.20
5.27
1542.25
839.18
83.99
1295.01
1.75
Sample Output
$1581.42
Source
Mid-Atlantic 2001


这一题是目前遇到的最简单的题目,求输入数组的平均值,注意如果使用scanf输入double的话,格式为scanf("%lf",&d),求得平均值之后,输出保留两位小数为printf("%.2f",d)。
代码如下:

#include<stdio.h>
int main()
{
     double rs=0,d;
     for(int i=0;i<12;i++)
     {
          scanf("%lf",&d);
          rs+=d;
     }
     printf("$%.2f",rs/12);
     return 0;
}

本代码提交AC,用时0MS,内存140K。