“科大讯飞杯”第18届上海大学程序设计联赛春季赛暨高校网络友谊赛

时间:2020-04-26
本文章向大家介绍“科大讯飞杯”第18届上海大学程序设计联赛春季赛暨高校网络友谊赛,主要包括“科大讯飞杯”第18届上海大学程序设计联赛春季赛暨高校网络友谊赛使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

C.最长非公共子序列

思路:思维题,有两段字符串,要求出最长不相同子序列的长度,

考虑到两个字符串完全相同的情况下是没有不相同子序列的,

而两个字符串不同时直接输出较长那个字符串的长度即可。

AC代码:

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
int main(){
    int i,m,n;
    char s1[10005],s2[10005];
    cin>>s1;
    cin>>s2;
    if(strcmp(s1,s2)==0){
        cout<<"-1"<<endl;
    }
    else{
        m=strlen(s1);
        n=strlen(s2);
        if(m>n)
            cout<<m<<endl;
        else
            cout<<n<<endl;
    }
    return 0;
}
D.最大字符集
思路:思维题,根据输入的n,要求出最多的01字符串,这些字符串满足相互不是子串,且任意两两长度不同
会发现只需分别考虑n<=2和n>2的情况,(没考虑n=2,wa了好几发>-<)
n=1时,任意输出0或1字符;
n=2时,任意输出0,11或1,00;
n>2时,只需首位与末尾均为0,中间位都为1,一共n-1项即可。
AC代码:
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<map>
#include<cstring>
using namespace std;
int main(){
    int n,i,j;
    cin>>n;
    if(n==1){
        cout<<"1"<<endl;
        cout<<"1"<<endl;
    }
    else if(n==2){
        cout<<"2"<<endl;
        cout<<"1"<<endl;
        cout<<"00"<<endl;
    }
    else{
        cout<<n-1<<endl;
        for(i=0;i<n-1;i++){
            cout<<"0";
            for(j=0;j<i;j++){
                cout<<"1";
            }
            cout<<"0"<<endl;
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/nanan/p/12782331.html