是小白呀~ 2020-11-30 22:16 采纳率: 33.3%
浏览 11

一直7分 就是ac不了 来个大神帮忙看看吧 求求了

18:验证子串

总时间限制: 

1000ms

 

内存限制: 

65536kB

描述

输入两个字符串,验证其中一个串是否为另一个串的子串。

输入

输入两个字符串, 每个字符串占一行,长度不超过200且不含空格。

输出

若第一个串s1是第二个串s2的子串,则输出(s1) is substring of (s2)
否则,若第二个串s2是第一个串s1的子串,输出(s2) is substring of (s1)
否则,输出 No substring。

样例输入

abc
dddncabca

样例输出

abc is substring of dddncabca
#include <stdio.h>
#include <string.h>
int main(){
	char a[200],b[200];
	gets(a);gets(b);
	int x=strlen(a),y=strlen(b);
	int i=0,j=0;
	while(a[i]!='\0'){
		if(a[i]==b[j]){
			i++;
			j++;
		}
		else{
			i=i-j+1;
			j=0;
		}
		if(j==y-1){
			printf("%s is substring of %s",b,a);
			return 0;
			
		}
	}
	i=0;j=0;
		while(b[i]!='\0'){
		if(b[i]==a[j]){
			i++;
			j++;
		}
		else{
			i=i-j+1;
			j=0;
		}
		if(j==x-1){
			printf("%s is substring of %s",a,b);
			return 0;
		}
	}
printf("No substring");
return 0;
} 
  • 写回答

1条回答 默认 最新

  • 关注

    AC代码:

    #include<bits/stdc++.h>
    using namespace std;
    string s1, s2; 
    int main(){
        cin >> s1 >> s2;
        if(s1.find(s2) != s1.npos){
            cout << s2 << " is substring of " << s1 << endl;
        }else if(s2.find(s1) != s2.npos){
            cout << s1 << " is substring of " << s2 << endl;
        }else{
            cout << "No substring\n";
        }
        return 0;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥30 为什么会失败呢,该如何调整
  • ¥15 前端返回pdf时不显示内容
  • ¥50 如何在不能联网影子模式下的电脑解决usb锁
  • ¥20 服务器redhat5.8网络问题
  • ¥15 如何利用c++ MFC绘制复杂网络多层图
  • ¥20 要做柴油机燃烧室优化 需要保持压缩比不变 请问怎么用AVL fire ESE软件里面的 compensation volume 来使用补偿体积来保持压缩比不变
  • ¥15 python螺旋图像
  • ¥15 算能的sail库的运用
  • ¥15 'Content-Type': 'application/x-www-form-urlencoded' 请教 这种post请求参数,该如何填写??重点是下面那个冒号啊
  • ¥15 找代写python里的jango设计在线书店