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.
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.
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).
ya 4 ah oy to ha
YES
hp 2 ht tp
NO
ah 1 ha
YES
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.
水。
if语句练习
#include <cstdio>
#include <iostream>
#include <string.h>
#include <string>
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int inf=0x3f3f3f3f;
const ll llinf=0x3f3f3f3f3f3f3f3f;
const ld pi=acos(-1.0L);
char a[105][5],s[5];
int main() {
int n,k,b,c,d;
cin >> n;
b=c=d=0;
scanf("%s",s);
scanf("%d",&n);
for (int i=1;i<=n;i++) {
scanf("%s",a[i]);
if (a[i][1]==s[0]) b=1;
if (a[i][0]==s[1]) c=1;
if (a[i][1]==s[1]&&a[i][0]==s[0]) d=1;
if (a[i][1]==s[0]&&a[i][0]==s[1]) d=1;
}
if (d) cout << "YES"; else if (b&&c) cout << "YES"; else cout << "NO";
return 0;
}
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.
Five integers h, m, s, t1, t2 (1 ≤ h ≤ 12, 0 ≤ m, s ≤ 59, 1 ≤ t1, t2 ≤ 12, t1 ≠ t2).
Misha's position and the target time do not coincide with the position of any hand.
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").
12 30 45 3 11
NO
12 0 1 12 1
YES
3 47 0 4 9
YES
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.
![](https://i-blog.csdnimg.cn/blog_migrate/1098aee6ce5f915964f097fa5659f313.png)
两个人站在钟表表盘的整数处,问在不跨越钟表时分秒针的前提之下,他们能否相遇。
对于走的慢的针,要考虑走的快的针对他的影响!
#include <cstdio>
#include <iostream>
#include <string.h>
#include <string>
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const ll llinf=0x3f3f3f3f3f3f3f3f;
const ld pi=acos(-1.0L);
db a[3];
int main() {
db h,m,s,c,b;
cin >> h >> m >> s >> b >> c;
h*=5;c*=5;b*=5;
if (h==60) h=0;
if (b==60) b=0;
if (c==60) c=0;
h+=s/720.0+m/12.0;
m+=s/60.0;
a[0]=h;a[1]=m;a[2]=s;
sort(a,a+3);
if (a[0]<=b&&b<=a[1]&&a[0]<=c&&c<=a[1]) cout << "YES"; else
if (a[1]<=b&&b<=a[2]&&a[1]<=c&&c<=a[2]) cout << "YES"; else
if ((b>=a[2]||b<=a[0])&&((c>=a[2]||c<=a[0]))) cout << "YES"; else
cout << "NO";
return 0;
}
有n个题目,k个队。给出每个队是否知道题目内容的01矩阵,问可不可以选择一些题,使得没有题目知道超过一半的题。
选择两个题的时候一定最优。
那么,最多4个队,最多16种状态,一个个枚举就好。
#include <cstdio>
#include <iostream>
#include <string.h>
#include <string>
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=100005,inf=0x3f3f3f3f;
const ll llinf=0x3f3f3f3f3f3f3f3f;
const ld pi=acos(-1.0L);
int a[maxn][5],b[5],c[20];
int main() {
int n,k,i,j,sum=0;
scanf("%d%d",&n,&k);
mem0(b);mem0(c);
for (i=1;i<=n;i++) {
int x=0;
for (j=1;j<=k;j++) {
scanf("%d",&a[i][j]);
if (a[i][j]) {
b[j]++,sum++;
x+=(1<<j-1);
}
}
c[x]++;
}
if (k==1) {
if (b[1]!=n) cout << "YES"; else cout << "NO";
} else {
if (c[0]!=0) {
cout << "YES";return 0;
}
for (i=0;i<16;i++) {
for (j=0;j<16;j++) {
if (i==j) continue;
if (((i&j)==0)&&c[i]&&c[j]) {
cout << "YES";return 0;
}
}
}
cout << "NO";
}
return 0;
}
分治+哈希。
详情请戳 这里
单调性优化DP。
详情 戳这里