天梯赛的一些题目

L1-064 估值一亿的AI核心代码 (20分)

自己写就拿了12分,
于是看了网上的代码学到了一些技巧
整体就是先找不是字母和数字的位子,插入空格,让他隔开来。也就实现了独立
然后再一个个放回去.
#include <bits/stdc++.h>
using namespace std;
int main(){
    int sum;
    cin>>sum;
    getchar();
    string a[sum];
    for(int i=0;i<sum;i++)
        getline(cin,a[i]);
    
    for(int i=0;i<sum;i++){
        string temp=a[i];
        cout<<temp<<endl<<"AI:";
        for(int j=0;j<temp.length();j++){
            if(isalnum(temp[j])){    //isalnum()判断是不是字母或者数字 
                if(temp[j]!='I')
                    temp[j]=tolower(temp[j]);     //tolower()变成小写字母 
            }
            else{                    //不是字母或数字就用空格隔开,这样就做到了把标点符号也当作独立的标准 
                temp.insert(j," ");
                j++;
            } 
            if(temp[j]=='?')
                temp[j]='!';
        }
        
        string str[1001],tmp;
        int flag=0;
        stringstream ss(temp);    //空格来存放分开的字符串 
        while(ss>>tmp){
            str[flag++]=tmp;
        }
        
        if(!isalnum(str[0][0]))    //如果第一个是其他字符,也需要空一格 
            cout<<" ";
        
        for(int i=0;i<flag;i++){
            if(!isalnum(str[i][0]))    //标点符号前不能有空格 
                cout<<str[i];
            else if(str[i]=="can" && str[i+1]=="you"){
                cout<<" I can";
                i++;
            }
            else if(str[i]=="could" && str[i+1]=="you"){
                cout<<" I could";
                i++;
            }
            else if(str[i]=="I" || str[i]=="me")
                cout<<" you";
            else
                cout<<" "<<str[i];
        }
        cout<<endl;
    }
    return 0;
} 

 L2-029 特立独行的幸福 (25分)

就硬模拟,想复杂了开始= =

#include <bits/stdc++.h>
using namespace std;
int a,b,tmp,ans[10007],anss[10007],re[100007],k;
int cal(int x){
    int res=0;
    while(x>0){
        int a=x%10;
        res+=a*a;
        x/=10;
    }
    return res;
}
bool isPrime[10007];
int Prime[10007],cnt=0;
void GetPrime(int n){
    memset(isPrime, 1, sizeof(isPrime));
    isPrime[1] = 0;
    for(int i = 2; i <= n; i++){
        if(isPrime[i])
            Prime[++cnt] = i;
        for(int j = 1; j <= cnt && i*Prime[j] <= n/*不超上限*/; j++) {
            isPrime[i*Prime[j]] = 0;
            if(i % Prime[j] == 0)    break;
        }
    }
}
int main(){
    scanf("%d%d",&a,&b);
    GetPrime(10007);
    for(int i=a;i<=b;++i){
        for(int j=1;j<=10000;++j)    re[j]=0;
        tmp=i;k=0;
        while(tmp){
            if(re[tmp]==1){
                ans[i]==-1;break;
            }
            re[tmp]=1;
            if(tmp==1&&ans[i]==0){
                ans[i]=1;anss[i]=k;break;
            }
            tmp=cal(tmp);
            ans[tmp]=-1;
            k++;
        }
    }
    int flag=1;
    for(int i=a;i<=b;++i){
        if(ans[i]==1){
            if(isPrime[i]==1)    printf("%d %d
",i,2*anss[i]);
            else        printf("%d %d
",i,anss[i]);
            if(flag)    flag=0;
        }
    } 
    if(flag)    printf("SAD
");
    return 0;
} 

L2-031 深入虎穴 (25分)

没啥好说的,就硬搜

不会建议从头学起

L2-032 彩虹瓶 (25分)

模拟,主要是栈

int n,m,k,pos,x,flag;
int main(){
    scanf("%d%d%d",&n,&m,&k);
    while(k--){
        stack<int> st;
        pos=flag=1;
        for(int i=1;i<=n;++i){
            scanf("%d",&x);
            if(x==pos){
                pos++;
                while(!st.empty()&&st.top()==pos){
                    st.pop();
                    pos++;
                }
            }
            else{
                st.push(x);
                if(st.size()>m){
                    flag=0;
                }
            }
        }
        if(st.size()==0&&flag==1) printf("YES
");
        else        printf("NO
");
    }
    return 0;
}

L2-030 冰岛人 (25分)

所谓“五代以内无公共祖先”是指两人的公共祖先(如果存在的话)必须比任何一方的曾祖父辈分高。

也就是说,我们必须找到其中一人的所有的祖先。

因为可能会出现,我们查找a和b,a的十代祖先是c,b的二代祖先是c,的这种情况。这种情况也是不行的。

原先写的代码因为一个点一直超时,所以参考了该博主的代码,十分感谢。

https://www.cnblogs.com/yuhan-blog/p/12308640.html

string a,b,c,d,e,f;
int n,x;
map<string,int>sex;
map<string,string>fa;
inline bool check(string & a, string & b) {
    map<string, int> rcd;
    for(int i = 1; a != ""; ++i, a = fa[a]) rcd[a] = i;    
    for(int i = 1; b != ""; ++i, b = fa[b]) {
        if(i>5&&rcd[a]>=5)    break;    //测试点6的超时原因
        if(rcd[b] && (rcd[b] < 5 || i < 5)) return false;
        if(rcd[b] >= 5) return true;    
    }
    return true;
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;++i){
        cin>>a>>b;
        c=a+' '+b;
        int len=c.length();
        if(c[len-1]=='m'){
            sex[c.substr(0,len-1)]=1;
        }
        else if(c[len-1]=='f'){
            sex[c.substr(0,len-1)]=-1;
        }
        else if(c[len-1]=='n'){
            sex[c.substr(0,len-4)]=1;
            fa[a]=b.substr(0,b.length()-4);
        }
        else if(c[len-1]=='r'){
            sex[c.substr(0,len-7)]=-1;
            fa[a]=b.substr(0,b.length()-7);
        }
    }
    scanf("%d",&x);
    while(x--){
        cin>>a>>b>>c>>d;
        e=a+' '+b;
        f=c+' '+d;
        if(sex[e]==0||sex[f]==0){
            printf("NA
");
            continue;
        }
        else if(sex[e]==sex[f]){
            printf("Whatever
");
            continue;
        }
        if(check(a,c)){
            printf("Yes
");
        }
        else{
            printf("No
");
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/PdrEam/p/13854869.html