admin管理员组

文章数量:1630208

D. Binary Literature

题意:

给三个长度为2 * n的01串,让你构造一个长度小于3 * n的字符串,使得这个串至少包含两个01串

题解:

很巧妙的构造题
三个指针分别指向三个串,因为是01串,所以一定存在两个字符串指针所指位置相等,相等部分加入S串中(S串为最后的答案),直到一个串完全结束
然后剩下分析如图

代码:

#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define me memset
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
using namespace std;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        string s1,s2,s3;
        cin>>s1>>s2>>s3;
        int len1=s1.length();
        int len2=s2.length();
        int len3=s3.length();
        int p1=0,p2=0,p3=0;
        string ans;
        while(p1<n*2&&p2<n*2&&p3<n*2)
        {
            if(s1[p1]==s2[p2])
            {
                ans+=s1[p1];
                p1++;
                p2++;
            }
            else if(s1[p1]==s3[p3])
            {
                ans+=s1[p1];
                p1++;
                p3++;
            }
            else
            {
                ans+=s2[p2];
                p2++;
                p3++;
            }
        }
       // cout<<ans<<endl;
        if(p1>=2*n)
        {
            if(p2>=p3) ans+=s2.substr(p2,2*n);
            else ans+=s3.substr(p3,2*n);
        }
        else if(p2>=2*n)
        {
            if(p1>=p3) ans+=s1.substr(p1,2*n);
            else ans+=s3.substr(p3,2*n);
        }
        else
        {
            if(p1>=p2) ans+=s1.substr(p1,2*n);
            else ans+=s2.substr(p2,2*n);
        }
        cout<<ans<<endl;
    }
    return 0;
}

本文标签: binaryLiterature