A. String Similarity(思维+构造)Educational Codeforces Round 94 (Rated for Div. 2)

原题链接: https://codeforces.com/contest/1400/problem/A

在这里插入图片描述
测试样例:

input
4
1
1
3
00000
4
1110000
2
101
output
1
000
1010
00

Note:

The explanation of the sample case (equal characters in equal positions are bold):

The first test case:
1 is similar to s[1…1]=1.

The second test case:
000 is similar to s[1…3]=000;
000 is similar to s[2…4]=000;
000 is similar to s[3…5]=000.

The third test case:
1010 is similar to s[1…4]=1110;
1010 is similar to s[2…5]=1100;
1010 is similar to s[3…6]=1000;
1010 is similar to s[4…7]=0000.

The fourth test case:
00 is similar to s[1…2]=10;
00 is similar to s[2…3]=01.

题意: 定义两个二进制字符串相似的规则为某一位置上字符相同。你会得到一个长度 ( 2 n − 1 ) (2n-1) (2n1)的二进制字符串。现你需构造一个长度为 n n n的二进制字符串 w w w,它相似于以下所有字符串: s [ 1.. n ] 、 s [ 2.. n + 1 ] 、 s [ 3.. n + 2 ] 、 … 、 s [ n . . 2 n − 1 ] s[1..n]、s[2..n+1]、s[3..n+2]、…、s[n..2n−1] s[1..n]s[2..n+1]s[3..n+2]s[n..2n1]

解题思路: 构造的思路就是寻求最简最优构造方案。我们先想想,怎么样才算相似?对应位置下标相同和字符相同。 那么,我们现在构造的二进制字符串需要相似 n n n个字符串,你可能会觉得有点不太现实,因为我们构造的字符串长度也才 n n n。那么我们应该怎样去想?我们看这 n n n个字符串,对其字符串编号。那么我们是不是都可以从第 i i i个字符串中取出第 i i i个字符构造。那么不就刚刚一一对应相似了吗? (即构造字符串中第 i i i个位置上的字符为与第 i i i个字符串相似的决定性因素。)这样我们就发现思路了,要记得字符串之间要跨越2才可以进入下一个字符串的下一个位置。具体实现看代码。

AC代码:

/*
*邮箱:unique_powerhouse@qq.com
*blog:https://me.csdn.net/hzf0701
*注:文章若有任何问题请私信我或评论区留言,谢谢支持。
*
*/
#include<bits/stdc++.h>	//POJ不支持

#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll>  pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//

int t,n;
string s;
int main(){
	//freopen("in.txt", "r", stdin);//提交的时候要注释掉
	IOS;
	while(cin>>t){
		while(t--){
			cin>>n>>s;
			for(int i=0;i<2*n-1;i+=2)
				cout<<s[i];
			cout<<endl;
		}
	}
	return 0;
}

posted @   unique_pursuit  阅读(7)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示