codevs 1204 寻找子串位置

时间:2023-03-09 04:41:24
codevs 1204 寻找子串位置

http://codevs.cn/problem/1204/

1204 寻找子串位置

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 青铜 Bronze
 查看运行结果
题目描述 Description

给出字符串a和字符串b,保证b是a的一个子串,请你输出b在a中第一次出现的位置。

输入描述 Input Description

仅一行包含两个字符串a和b

输出描述 Output Description

仅一行一个整数

样例输入 Sample Input

abcd bc

样例输出 Sample Output

2

数据范围及提示 Data Size & Hint

字符串的长度均不超过100

Pascal用户请注意:两个字符串之间可能包含多个空格

分析:

数据量不是很大,直接对字符串进行匹配即可。

AC代码:

 /*
作者:1348066599@qq.com
题目:p1204 寻找子串位置
*/ #include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <list>
#include <iomanip>
#include <vector>
#pragma comment(linker, "/STACK:1024000000,1024000000")
#pragma warning(disable:4786) using namespace std; const int INF = 0x3f3f3f3f;
const int MAX = + ;
const double eps = 1e-;
const double PI = acos(-1.0); int main()
{
char str[] , str1[];
while(cin >> str >> str1)
{
int i , j , ans = ;
int len = strlen(str);
int len1 = strlen(str1);
for(i = ;i < len - len1 + ;i ++)
{
int ii = i , k = ;
for(j = ;j < len1;j ++)
if(str1[j] == str[ii ++])
k ++;
else
break;
if(j == len1)
{
ans = i + ;
break;
}
}
printf("%d\n",ans);
}
return ;
}