Educational Codeforces Round 94 (Rated for Div. 2) 賽後總結及題解
阿新 • • 發佈:2020-08-28
題意:定義若兩個長度相同的01串中,存在某一個位置,使得兩字串在該位置的字元相同(a[i]=b[i]),則稱這兩個字串相似。現在給一個長度為2*n-1的01字串s,要求構造一個長度為n的字串,使得該字串與給出字串中的s[1..n],s[2..n+1],s[3..n+2],......,s[n..2*n-1]均相似。
思路:對於每一對相似字串,只要有一個位置上的字元相同即可。
於是對於s[1..n]取出第一位,對於s[2..n+1]取出第二位......即s[1],s[3],s[5],......,s[2*n-1]組合起來,就可構造出答案。
#include<cstdio> #includeView Code<cstring> #include<algorithm> #define maxn 55 #define mod 100000000 typedef long long ll; using namespace std; char s[maxn]; int main() { int T,i,n,len; scanf("%d",&T); while (T--) { scanf("%d%s",&n,s); len=2*n-1; for (i=0;i<len;i+=2) { printf("%c",s[i]); } printf("\n"); } return 0; }
C Binary String Reconstruction
題意:給一個01串s,要求輸出它對應的01串w,若不存在符合條件的w串則輸出-1。其中若w[i-x]==1或w[i+x]==1時,s[i]為1;若不滿足則s[i]=0。
思路:對於s串中每一個0,都有w[i-x]==0且w[i+x]==0(若存在)。
因此預處理將w的每一位賦值為1,先將s中的所有0對應的w[i-x]和w[i+x]標記為0,再處理s中的每一位1,若對應的w[i-x]與w[i+x]均被標記為0,說明不存在符合條件的w串,輸出-1。
最後直接輸出w串就可以了。
#include<cstdio> #include<cstring> #include<algorithm> #define maxn 100010 typedef long long ll; using namespace std; int ans[maxn]; char s[maxn]; int main() { int T,i,x,len,flag,cnt; scanf("%d",&T); while (T--) { scanf("%s%d",s,&x); len=strlen(s); flag=0; for (i=0;i<len;i++) ans[i]=1; for (i=0;i<len;i++) { if (s[i]=='0') { if (i-x>=0) ans[i-x]=0; if (i+x<len) ans[i+x]=0; } } for (i=0;i<len;i++) { if (s[i]=='1') { cnt=0; if (i-x<0 || ans[i-x]==0) cnt++; if (i+x>=len || ans[i+x]==0) cnt++; if (cnt==2) { flag=1;break; } } } if (flag) { printf("-1\n"); continue; } else { for (i=0;i<len;i++) printf("%d",ans[i]); printf("\n"); } } return 0; }View Code
D Zigzags