比賽題解 (2)—— 思維
The preferred way to generate user login in Polygon is to concatenate a prefix of the user's first name and a prefix of their last name, in that order. Each prefix must be non-empty, and any of the prefixes can be the full name. Typically there are multiple possible logins for each person.
You are given the first and the last name of a user. Return the alphabetically earliest login they can get (regardless of other potential Polygon users).
As a reminder, a prefix of a string s is its substring which occurs at the beginning of s: "a", "ab", "abc" etc. are prefixes of string "{abcdef}" but "b" and 'bc" are not. A string a is alphabetically earlier than a string b, if a is a prefix of b, or a and b coincide up to some position, and then a
Input
The input consists of a single line containing two space-separated strings: the first and the last names. Each character of each string is a lowercase English letter. The length of each string is between 1 and 10, inclusive.
Output
Output a single string — alphabetically earliest possible login formed from these names. The output should be given in lowercase as well.
題意:
名字中有 first name and last name ,現在有一個登入使用者名稱,要求是這兩個名字的字首組合起來的
現在求一個最早的登入使用者名稱,何為最早?
1、這個字串是其他登入使用者民的字首(即是一個最短的)
3、必須包含 first name 的首字母 and last name 的首字母 (因為由這兩個的字首組成的,所以最少得包含首字母)
2、這個字串是由 first name and last name 的字首組成的,要滿足 first name 中的字首小於 last name 中的字母,按字典序由 小到大
這三點是由這一段得出來的,當時也是看了好久才明白
A string a is alphabetically earlier than a string b, if a(字串) is a prefix(字首的意思) of b(字串), or a and b coincide up (重合)to some position, and then a has a letter (a 字串有一個字母 a)that is alphabetically earlier than the corresponding letter in b(比相應的字母 b 要排序在前,就是字典序在前 )
思路:
首先將 first name and last name 的首字母分別固定在 新字串的首位和末尾,然後在 first name 中找 小於(不是等於,若判斷是等於的話,它不是 不包含等於末尾 的字首 ) last name 首字母的插入到新字串中,最後輸出新字串即可
Code:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
int main()
{
string s,c,ss;
s.clear();
c.clear();
ss.clear();
cin>>s;
cin>>c;
ss[0]=s[0];
int flag=1;
for(int i=1;s[i];i++){
if(s[i]<c[0])
ss[flag++]=s[i];
else
break;
}
ss[flag++]=c[0];
ss[flag]='\0';
for(int i=0;i<flag;i++)
cout<<ss[i];
cout<<endl;
}
詳解這篇部落格
Mishka has got n empty boxes. For every i (1 ≤ i ≤ n), i-th box is a cube with side length ai.
Mishka can put a box i into another box j if the following conditions are met:
- i-th box is not put into another box;
- j-th box doesn't contain any other boxes;
- box i is smaller than box j (ai < aj).
Mishka can put boxes into each other an arbitrary number of times. He wants to minimize the number of visible boxes. A box is called visible iff it is not put into some another box.
Help Mishka to determine the minimum possible number of visible boxes!
Input
The first line contains one integer n (1 ≤ n ≤ 5000) — the number of boxes Mishka has got.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the side length of i-th box.
Output
Print the minimum possible number of visible boxes.
Examples
Input
3
1 2 3
Output
1
Input
4
4 2 4 3
Output
2
Note
In the first example it is possible to put box 1 into box 2, and 2 into 3.
In the second example Mishka can put box 2 into box 3, and box 4 into box 1.
Vova is again playing some computer game, now an RPG. In the game Vova's character received a quest: to slay the fearsome monster called Modcrab.
After two hours of playing the game Vova has tracked the monster and analyzed its tactics. The Modcrab has h2 health points and an attack power of a2. Knowing that, Vova has decided to buy a lot of strong healing potions and to prepare for battle.
Vova's character has h1 health points and an attack power of a1. Also he has a large supply of healing potions, each of which increases his current amount of health points by c1 when Vova drinks a potion. All potions are identical to each other. It is guaranteed that c1 > a2.
The battle consists of multiple phases. In the beginning of each phase, Vova can either attack the monster (thus reducing its health by a1) or drink a healing potion (it increases Vova's health by c1; Vova's health can exceed h1). Then, if the battle is not over yet, the Modcrab attacks Vova, reducing his health by a2. The battle ends when Vova's (or Modcrab's) health drops to 0 or lower. It is possible that the battle ends in a middle of a phase after Vova's attack.
Of course, Vova wants to win the fight. But also he wants to do it as fast as possible. So he wants to make up a strategy that will allow him to win the fight after the minimum possible number of phases.
Help Vova to make up a strategy! You may assume that Vova never runs out of healing potions, and that he can always win.
Input
The first line contains three integers h1, a1, c1 (1 ≤ h1, a1 ≤ 100, 2 ≤ c1 ≤ 100) — Vova's health, Vova's attack power and the healing power of a potion.
The second line contains two integers h2, a2 (1 ≤ h2 ≤ 100, 1 ≤ a2 < c1) — the Modcrab's health and his attack power.
Output
In the first line print one integer n denoting the minimum number of phases required to win the battle.
Then print n lines. i-th line must be equal to HEAL if Vova drinks a potion in i-th phase, or STRIKE if he attacks the Modcrab.
The strategy must be valid: Vova's character must not be defeated before slaying the Modcrab, and the monster's health must be 0 or lower after Vova's last action.
If there are multiple optimal solutions, print any of them.
Examples
Input
10 6 100
17 5
Output
4
STRIKE
HEAL
STRIKE
STRIKE
Input
11 6 100
12 5
Output
2
STRIKE
STRIKE
題意:
Vova的角色有 H1 的健康點和 A1 的攻擊力。此外,他有大量的治療藥劑 c1 ,每一個都增加了他目前的健康點量
Vova可以攻擊怪物,從而減少怪物 A1 的健康點,或喝治療藥水,它增加了Vova的健康C1
怪物可以攻擊Vova,減少 Vova A 2 的健康點
開始理解錯了題意,以為殺一次怪物會減少 A2 的健康點,其實是
殺一次怪物會 減少怪物 A1 的健康點
重點:
當Vova 健康值小於 攻擊後的減少值(攻擊一次之後使血量 減少至 0一下)
或者是 怪物的健康值大於 減少值的時候
需要增加血量,進行HEAL操作
否則進行STRIKE操作
CODE:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int h,a,c;
int h1,a1;
int flag=0;
int ans[20002];
memset(ans,0,sizeof(ans));
scanf("%d %d %d",&h,&a,&c);
scanf("%d %d",&h1,&a1);
while(h1>0)
{
if(h<=a1 && a<h1)
{
ans[flag++]=2;
h+=c;
h-=a1;
}
else
{
ans[flag++]=1;
h-=a1;
h1-=a;
}
}
cout<<flag<<endl;
for(int i=0; i<flag; i++)
{
if(ans[i]==2)
printf("HEAL\n");
if(ans[i]==1)
printf("STRIKE\n");
}
}
Everybody in Russia uses Gregorian calendar. In this calendar there are 31 days in January, 28 or 29 days in February (depending on whether the year is leap or not), 31 days in March, 30 days in April, 31 days in May, 30 in June, 31 in July, 31 in August, 30 in September, 31 in October, 30 in November, 31 in December.
A year is leap in one of two cases: either its number is divisible by 4, but not divisible by 100, or is divisible by 400. For example, the following years are leap: 2000, 2004, but years 1900 and 2018 are not leap.
In this problem you are given n (1 ≤ n ≤ 24) integers a1, a2, ..., an, and you have to check if these integers could be durations in days of n consecutive months, according to Gregorian calendar. Note that these months could belong to several consecutive years. In other words, check if there is a month in some year, such that its duration is a1 days, duration of the next month is a2 days, and so on.
Input
The first line contains single integer n (1 ≤ n ≤ 24) — the number of integers.
The second line contains n integers a1, a2, ..., an (28 ≤ ai ≤ 31) — the numbers you are to check.
Output
If there are several consecutive months that fit the sequence, print "YES" (without quotes). Otherwise, print "NO" (without quotes).
You can print each letter in arbitrary case (small or large).
Examples
Input
4
31 31 30 31
Output
Yes
Input
2
30 30
Output
No
Input
5
29 31 30 31 30
Output
Yes
Input
3
31 28 30
Output
No
Input
3
31 31 28
Output
Yes
Note
In the first example the integers can denote months July, August, September and October.
In the second example the answer is no, because there are no two consecutive months each having 30 days.
In the third example the months are: February (leap year) — March — April – May — June.
In the fourth example the number of days in the second month is 28, so this is February. March follows February and has 31 days, but not 30, so the answer is NO.
In the fifth example the months are: December — January — February (non-leap year).
題意:
給定 n 個月份,判斷是否在某一年中存在這幾個月份
思路:
我是直接模擬的
定義一個常量陣列,存12個月份,用一點小技巧,將 2 月份 存 0,減少了判斷是閏年平年的麻煩
由於給定的陣列長度是 24,就兩年,這兩年需要滿足:
1、不能是連著出現兩年的 閏年
2、可以是 閏年平年 或者 平年閏年
思路:
當判斷 第一個和陣列中的某位數相同時,再開一個迴圈 依次判斷是否相等
CODE:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<set>
#include<map>
#include<algorithm>
#include<queue>
#include<stack>
typedef long long LL;
using namespace std;
#define INF 0x3f3f3f3f
int main()
{
int n;
int a[30];
int sum=0,sum1=0;
int b[20]= {0,31,0,31,30,31,30,31,31,30,31,30,31};
// int c[20]= {0,31,29,31,30,31,30,31,31,30,31,30,31};
int flag=0;
scanf("%d",&n);
if(n==1)
printf("Yes\n");
else
{
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
if(a[i]==28)
{
// flag=1;
a[i]=0; //將 2月份 設定成陣列中的0
sum1++;
}
if(a[i]==29)
{
// kk=i;
//flag=2;
a[i]=0;
sum++;
}
}
int t=0;
if(sum>1)
printf("No\n"); // 不能連續出現兩年閏年
else
{
sum=0;
int j=1;
int x,y;
for(int i=1; i<25; i++)
{
sum=0;
if(i%12==0)
x=12;
else
x=i%12; // 迴圈數組裡的數
if(a[1]==b[x])
{
sum++;
for(int k=2; k<25; k++)
{
x++;
if(x%12==0)
x==12;
else
x=x%12;
if(a[k]==b[x])
{
sum++;
if(sum==n)
{
t=1;
break;
}
}
else
break;
}
}
if(t==1)
break;
}
if(t==1)
printf("Yes\n");
else
printf("No\n");
}
}
}