【问题描述】
输入两个字符串,一个s,一个t,在s中找到匹配t的子串,并输出该子串第一个字符的位置
输出为一行,含一个整数,即匹配的第一个位置,如果没找到,输出-1
【输入形式】
第一行输入字符串s,长度不超过10000
第二行输入字符串t,长度不超过10000
【输出形式】
输出一个整数
【样例输入1】
helloworld world
【样例输出1】
5
【样例输入2】
987654321world 2world
【样例输出2】
-1
【样例说明】
样例1: 字符串helloworld中,有world的匹配,其中匹配位置的起始点在于字符w,而w位置是第5个(字符h是位置0),因此输出5 样例2: 字符串987654321world中没有2world的匹配,因此输出-1
【评分标准】
通过所有测试用例
#include<iostream>
using namespace std;
int main()
{
string s,t;
int cnt=0,flag=0;
cin>>s>>t;
for(int i=0;i<s.length();i++)
{
for(int j=0;j<t.length();j++)
{
if(t[j]==s[i+j]) cnt++;
else cnt=0;
if(cnt==t.length()) flag=1,cout<<cnt;
}
}
if(flag==0) cout<<-1;
}