A - Prefix and Suffix
Time limit : 2sec / Memory limit : 256MB
Score : 200 points
Problem Statement
Snuke is interested in strings that satisfy the following conditions:
The length of the string is at least N.
The first N characters equal to the string s.
The last N characters equal to the string t.
Find the length of the shortest string that satisfies the conditions.
Constraints
1≤N≤100
The lengths of s and t are both N.
s and t consist of lowercase English letters.
Input
The input is given from Standard Input in the following format:
N
s
t
Output
Print the length of the shortest string that satisfies the conditions.
Sample Input 1
3
abc
cde
Sample Output 1
5
The shortest string is abcde.
Sample Input 2
1
a
z
Sample Output 2
2
The shortest string is az.
Sample Input 3
4
expr
expr
Sample Output 3
4
N才100,于是N^2暴力模拟吧
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#include<map>
#define For(i,a,b) for(int i=a;i<=b;i++)
#define ForD(i,a,b) for (int i=a;i>=b;i--)
#define LL long long
#define INF (2147483647)
#define sqr(x) ((x)*(x))
using namespace std;
inline int read(){
int data=0,w=1; char ch=0;
while (ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if (ch=='-') w=-1,ch=getchar();
while (ch>='0' && ch<='9') data=data*10+ch-'0',ch=getchar();
return data*w;
}
inline void write(int x){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10+'0');
}
char s1[200],s2[200];
int t;
int main(){
int n=read();
scanf("%s",s1+1);
scanf("%s",s2+1);
For(i,1,n+1){
bool t=true;
For(j,i,n)
if (s1[j]!=s2[j-i+1]){
t=false;break;
}
if (t){
write((i-1)*2+(n-i+1)),puts("");
break;
}
}
}
B - Median Pyramid Easy
Time limit : 2sec / Memory limit : 256MB
Score : 400 points
Problem Statement
We have a pyramid with N steps, built with blocks. The steps are numbered 1 through N from top to bottom. For each 1≤i≤N, step i consists of 2i−1 blocks aligned horizontally. The pyramid is built so that the blocks at the centers of the steps are aligned vertically.
A pyramid with N=4 steps
Snuke wrote a permutation of (1, 2, …, 2N−1) into the blocks of step N. Then, he wrote integers into all remaining blocks, under the following rule:
The integer written into a block b must be equal to the median of the three integers written into the three blocks directly under b, or to the lower left or lower right of b.
Writing integers into the blocks
Afterwards, he erased all integers written into the blocks. Now, he only remembers that the integer written into the block of step 1 was x.
Construct a permutation of (1, 2, …, 2N−1) that could have been written into the blocks of step N, or declare that Snuke's memory is incorrect and such a permutation does not exist.
Constraints
2≤N≤10^5
1≤x≤2N−1
Input
The input is given from Standard Input in the following format:
N x
Output
If no permutation of (1, 2, …, 2N−1) could have been written into the blocks of step N, print No.
Otherwise, print Yes in the first line, then print 2N−1 lines in addition.
The i-th of these 2N−1 lines should contain the i-th element of a possible permutation.
Sample Input 1
4 4
Sample Output 1
Yes
1
6
3
7
4
5
2
This case corresponds to the figure in the problem statement.
Sample Input 2
2 1
Sample Output 2
No
No matter what permutation was written into the blocks of step N, the integer written into the block of step 1 would be 2.
首先来想想什么情况是不可能的?
x=1||x=2n-1时
那么其他情况呢?
当我们要获得x时,只需要下一层的两个数为x,那就这需要下下一层的两个数为x…………
所以我们最后只要构造第n层的4个数就可以了啊(脑洞大开啊)我想,知道怎么构造还WA一发的也就我了吧
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#include<map>
#define For(i,a,b) for(int i=a;i<=b;i++)
#define ForD(i,a,b) for (int i=a;i>=b;i--)
#define LL long long
#define INF (2147483647)
#define sqr(x) ((x)*(x))
using namespace std;
inline int read(){
int data=0,w=1; char ch=0;
while (ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if (ch=='-') w=-1,ch=getchar();
while (ch>='0' && ch<='9') data=data*10+ch-'0',ch=getchar();
return data*w;
}
inline void write(int x){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10+'0');
}
int a[1000000],f[1000000];
int main(){
int n=read(),x=read();
if (x==1||x==2*n-1) {
puts("No");return 0;
}
if (n==2){
puts("Yes");
For(i,1,3)
write(i),puts("");
return 0;
}
memset(f,true,sizeof(f));
if (x>2){
f[1]=false,f[2]=false,f[x+1]=false,f[x]=false;
a[n-1]=1,a[n]=x,a[n+1]=x+1,a[n+2]=2;
int t=1;
For(i,1,2*n-1){
if (a[i]!=0) continue;
while(!f[t])
t++;
a[i]=t;t++;
}
}
else{
f[1]=false,f[2]=false,f[3]=false,f[4]=false;
a[n-1]=3,a[n]=2,a[n+1]=1,a[n+2]=4;
int t=1;
For(i,1,2*n-1){
if (a[i]!=0) continue;
while(!f[t])
t++;
a[i]=t;t++;
}
}
puts("Yes");
For(i,1,2*n-1)
write(a[i]),puts("");
}