数据结构实验之串二:字符串匹配
Time Limit: 1000MS Memory limit: 65536K
题目描述
给定两个字符串string1和string2,判断string2是否为string1的子串。
输入
输入包含多组数据,每组测试数据包含两行,第一行代表string1,第二行代表string2,string1和string2中保证不出现空格。(string1和string2大小不超过100字符)
输出
对于每组输入数据,若string2是string1的子串,则输出"YES",否则输出"NO"。
示例输入
abc a 123456 45 abc ddd
示例输出
YES YES NO
提示
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
using namespace std;
int substring(char a[],char b[])
{
int n=strlen(a);
int m=strlen(b);
int i=0,j=0;
while(i<n-m+1&&j<m)
{
if(a[i+j]==b[j])
{
j++;
}
else
{
j=0;
i++;
}
}
if(b[j]=='\0')
cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
int main()
{
char a[110],b[110];
while(cin>>a>>b)
{
int o=substring(a,b);
}
}