Codeforces Round #438 by Sberbank and Barcelona Bootcamp (Div. 1 + Div. 2 combined) A-C题解

原创 2017年10月05日 21:10:08
A. Bark to Unlock
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

As technologies develop, manufacturers are making the process of unlocking a phone as user-friendly as possible. To unlock its new phone, Arkady's pet dog Mu-mu has to bark the password once. The phone represents a password as a string of two lowercase English letters.

Mu-mu's enemy Kashtanka wants to unlock Mu-mu's phone to steal some sensible information, but it can only bark n distinct words, each of which can be represented as a string of two lowercase English letters. Kashtanka wants to bark several words (not necessarily distinct) one after another to pronounce a string containing the password as a substring. Tell if it's possible to unlock the phone in this way, or not.

Input

The first line contains two lowercase English letters — the password on the phone.

The second line contains single integer n (1 ≤ n ≤ 100) — the number of words Kashtanka knows.

The next n lines contain two lowercase English letters each, representing the words Kashtanka knows. The words are guaranteed to be distinct.

Output

Print "YES" if Kashtanka can bark several words in a line forming a string containing the password, and "NO" otherwise.

You can print each letter in arbitrary case (upper or lower).

Examples
input
ya
4
ah
oy
to
ha
output
YES
input
hp
2
ht
tp
output
NO
input
ah
1
ha
output
YES
Note

In the first example the password is "ya", and Kashtanka can bark "oy" and then "ah", and then "ha" to form the string "oyahha" which contains the password. So, the answer is "YES".

In the second example Kashtanka can't produce a string containing password as a substring. Note that it can bark "ht" and then "tp" producing "http", but it doesn't contain the password "hp" as a substring.

In the third example the string "hahahaha" contains "ah" as a substring.

给定字符串,给定n个字符串,问是否可以组成子串同原串一样,每个字符串的长度都是2.

模拟。

代码实现:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstdio>
#define ll long long
#define mset(a,x) memset(a,x,sizeof(a))

using namespace std;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=100005;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}
ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}

int main()
{
	int i,j,k,n;
	char a,b,c,d;
	string map[105];
	while(cin>>a>>b)
	{
		cin>>n;
		int flag=0,flag2=0;
		for(i=0;i<n;i++)
		{
			cin>>map[i];
		}
		
		for(i=0;i<n&&!flag;i++)
		{
			if(map[i][0]==a&&map[i][1]==b)
			{
				flag=1;
				break;
			}
			for(j=0;j<n&&!flag;j++)
			{
				if(map[i][1]==a&&map[j][0]==b)
				{
					flag=1;
					break;
				}
			}
		}
		if(flag)
		cout<<"YES"<<endl;
		else
		cout<<"NO"<<endl;
	}
	return 0;
}

B. Race Against Time
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Have you ever tried to explain to the coordinator, why it is eight hours to the contest and not a single problem has been prepared yet? Misha had. And this time he has a really strong excuse: he faced a space-time paradox! Space and time replaced each other.

The entire universe turned into an enormous clock face with three hands — hour, minute, and second. Time froze, and clocks now show the time h hours, m minutes, s seconds.

Last time Misha talked with the coordinator at t1 o'clock, so now he stands on the number t1 on the clock face. The contest should be ready by t2 o'clock. In the terms of paradox it means that Misha has to go to number t2 somehow. Note that he doesn't have to move forward only: in these circumstances time has no direction.

Clock hands are very long, and Misha cannot get round them. He also cannot step over as it leads to the collapse of space-time. That is, if hour clock points 12 and Misha stands at 11 then he cannot move to 1 along the top arc. He has to follow all the way round the clock center (of course, if there are no other hands on his way).

Given the hands' positions, t1, and t2, find if Misha can prepare the contest on time (or should we say on space?). That is, find if he can move from t1 to t2 by the clock face.

Input

Five integers hmst1t2 (1 ≤ h ≤ 120 ≤ m, s ≤ 591 ≤ t1, t2 ≤ 12t1 ≠ t2).

Misha's position and the target time do not coincide with the position of any hand.

Output

Print "YES" (quotes for clarity), if Misha can prepare the contest on time, and "NO" otherwise.

You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").

Examples
input
12 30 45 3 11
output
NO
input
12 0 1 12 1
output
YES
input
3 47 0 4 9
output
YES
Note

The three examples are shown on the pictures below from left to right. The starting position of Misha is shown with green, the ending position is shown with pink. Note that the positions of the hands on the pictures are not exact, but are close to the exact and the answer is the same.


给定一个时间,这个时间的指针在钟表上是固定的,给定两个整数点,可以顺时针,也可以逆时针的跑,问途中是否会遇到钟表指针。

转化为角度,模拟。

代码实现:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstdio>
#define ll long long
#define mset(a,x) memset(a,x,sizeof(a))

using namespace std;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=100005;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}
ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}

int solve(double a,double b,double c)
{
	if(c>=a&&c<=b)
	return 1;
	else
	return 0;
}

int main()
{
	int h,t1,t2,i,j,k;
	int m,s;
	while(cin>>h>>m>>s>>t1>>t2)
	{
		if(t1>t2)
		swap(t1,t2);
		
		h=h%12;
		double a=t1*30.0;
		double b=t2*30.0;
		double c=(h*30)+m*0.5+s/120.0;
		double d=m*6.0+s*1.0/60.0;
		double e=s*6.0;
		
		int ans=0;
		ans+=solve(a,b,c);
		ans+=solve(a,b,e);
		ans+=solve(a,b,d);
		if(ans==0||ans==3)
		cout<<"YES"<<endl;
		else
		cout<<"NO"<<endl;
	}
	return 0;
}

C. Qualification Rounds
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of nproblems, and they want to select any non-empty subset of it as a problemset.

k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.

Determine if Snark and Philip can make an interesting problemset!

Input

The first line contains two integers nk (1 ≤ n ≤ 1051 ≤ k ≤ 4) — the number of problems and the number of experienced teams.

Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.

Output

Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.

You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").

Examples
input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
output
NO
input
3 2
1 0
1 1
0 1
output
YES
Note

In the first example you can't make any interesting problemset, because the first team knows all problems.

In the second example you can choose the first and the third problems.


给定n门课,有k个有经验的人,出一套题,要使每个队至少有一半以上的人没做过,问最后是否存在这样的题。

由于k最大为4,也就是状态最多为2^4=16种,可是n最大是10^5这样会有很多重复状态的题,所以将其去重,进行状态压缩,枚举两题出现这种情况即可,两层循环最多16*16的耗时,只需&运算一下,若等于0,则输出答案YES,否则输出NO。

代码实现:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstdio>
#define ll long long
#define mset(a,x) memset(a,x,sizeof(a))

using namespace std;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e5+5;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}
ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}
int visit[20];

int main()
{
	int n,k,i,j,a;
	while(cin>>n>>k)
	{
		mset(visit,0);
		for(i=0;i<n;i++)
		{
			int ans=0;
			for(j=0;j<k;j++)
			{
				cin>>a;
				ans=ans*2+a;
			}
			visit[ans]++;
		}
		
		for(i=0;i<pow(2,k);i++)
		{
			for(j=0;j<pow(2,k);j++)
			{
				if(visit[i]>0&&visit[j]>0&&((i&j)==0))
				{
					cout<<"YES"<<endl;
					goto q;
				}
			}
		}
		cout<<"NO"<<endl;
q:		;
	}
	return 0;
}


版权声明:欢迎转载,请注明此博客地址。

Codeforces Round #438 by Sberbank and Barcelona Bootcamp (Div. 1 + Div. 2 combined)

A. Bark to Unlock#include using namespace std; #define For(i,n) for(int i=1;i

Codeforces Round #438 by Sberbank and Barcelona Bootcamp (Div. 1 + Div. 2 combined)C - Qualification

C. Qualification Rounds time limit per test 2 seconds memory limit per test 256 megabyte...

Codeforces Round #438 by Sberbank and Barcelona Bootcamp (Div. 1 + Div. 2 combined)

A #include #include #include #include #include #include #include #include #include #includ...

Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined)

A. Neverending competitionstime limit per test:2 secondsmemory limit per test:512 megabytesinput:sta...

Editorial Divide by Zero and Codeforces Round #399 (Div. 1+2, combined) (A~F)

CF #399 A题:水题 代码: #include using namespace std; int a[100010]; int main() { int n; cin>>n; for(i...

ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) 题解(A-E)

A A Serial Killer#include using namespace std; #define For(i,n) for(int i=1;i

Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined)

这把比赛题目不错,然而打得太挫了。。来写一发题解C. Jon Snow and his Favourite Number基本上是暴力模拟,不过排序和异或要写O(1000)O(1000)的。#inclu...

Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) C. Felicity is Coming!组合学+集合

题意:给出n组数,每组gi个数,每个数属于1~m,每个数可以变化但变化前相同的数变化后依然相同,变化前不同的速变化后依然不同,且可能不变,但经过变化后每组的每种数的个数不会变化,求变化的总方案数。 组...

Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) -- C. Felicity is Coming! (STL水过)

大体题意: 有n 个体育馆每个体育馆里有 不同的精灵同样数字的精灵进化的结果是一样的不同数字进化的结果一定不同有多少个不同的进化方法使得每个体育馆进化后 和原来一样? 思路: 两个精灵可以互...

Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) C. Felicity is Coming! 哈希

解法:对于pokermon a和b,只要它们在所有gym中出现的次数都是相同的,则变身的时候,种类可以互换。没有出现过的pokermon,归为所有gym都是0的情况,比赛的时候特判了这个反而没有过pr...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Codeforces Round #438 by Sberbank and Barcelona Bootcamp (Div. 1 + Div. 2 combined) A-C题解
举报原因:
原因补充:

(最多只允许输入30个字)