2019 Xuzhou D. Carneginon(kmp - 子串 / stl - string)

链接:https://nanti.jisuanke.com/t/41386
来源:The Preliminary Contest for ICPC Asia Xuzhou 2019

在这里插入图片描述

  题意:给我们两个字符串 t s,1. lent == lens:t == s 输出 jntm! 否则输出 friend! 2. lent > lens s 是 t 的子串 输出 my child! 否则输出 oh, child! 3. lent ≤ lens t 是 s 的子串 输出 my teacher! 否则输出 senior!

//stl - string
#include<bits/stdc++.h>
using namespace std;

int main(){
    ios::sync_with_stdio(false);
    string t;int q;
    cin>>t>>q;
    int lent=t.length();
    while(q--){
        string s;
        cin>>s;
        int lens=s.length();
        if(lens==lent){
            if(s==t) cout<<"jntm!"<<'\n';
            else cout<<"friend!"<<'\n';
        }else if(lent>lens){
            string c=t.substr(0,lens);
            if(c==s) cout<<"my child!"<<'\n';
            else cout<<"oh, child!"<<'\n';
        }else{
            string c=s.substr(0,lent);
            if(c==t) cout<<"my teacher!"<<'\n';
            else cout<<"senior!"<<'\n';
        }
    }
    return 0;
}
//kmp - 子串位置
#include<bits/stdc++.h>
using namespace std;

const int Max_n=1e5+10;
char t[Max_n],s[Max_n];
int net[Max_n];

void get_net(int t,char *b){
	net[0]=-1;
	for(int i=0,j=-1;i<t;){
		if(j==-1||b[i]==b[j]){
            i++;j++;
			net[i]=j;
		}else j=net[j];
	}
}

int kmp(int s,int t,char *a,char *b){
	int i=0,j=0;
	while(i<s&&j<t){
		if(j==-1||a[i]==b[j]){
			i++;j++;
		}else j=net[j];
	}
    return j>=t?i-t:-1;
}

int main(){
    scanf("%s",t);
    int q; scanf("%d",&q);
    while(q--){
        scanf("%s",s);
        int lent=strlen(t),lens=strlen(s);
        if(lent==lens){ get_net(lens,s);
            printf("%s\n",kmp(lent,lens,t,s)==-1?"friend!":"jntm!");
        }else if(lent>lens){ get_net(lens,s);
            printf("%s\n",kmp(lent,lens,t,s)==-1?"oh, child!":"my child!");
        }else{ get_net(lent,t);
            printf("%s\n",kmp(lens,lent,s,t)==-1?"senior!":"my teacher!");
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值