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代码:

 1 /*
 2 作者:1348066599@qq.com
 3 题目:p1204 寻找子串位置
 4 */
 5 
 6 #include <stdio.h>
 7 #include <algorithm>
 8 #include <iostream>
 9 #include <string.h>
10 #include <string>
11 #include <math.h>
12 #include <stdlib.h>
13 #include <queue>
14 #include <stack>
15 #include <set>
16 #include <map>
17 #include <list>
18 #include <iomanip>
19 #include <vector>
20 #pragma comment(linker, "/STACK:1024000000,1024000000")
21 #pragma warning(disable:4786)
22 
23 using namespace std;
24 
25 const int INF = 0x3f3f3f3f;
26 const int MAX = 10000 + 10;
27 const double eps = 1e-8;
28 const double PI = acos(-1.0);
29 
30 int main()
31 {
32     char str[101] , str1[101];
33     while(cin >> str >> str1)
34     {
35         int i , j , ans = 0;
36         int len = strlen(str);
37         int len1 = strlen(str1);
38         for(i = 0;i < len - len1 + 1;i ++)
39         {
40             int ii = i , k = 0;
41             for(j = 0;j < len1;j ++)
42                 if(str1[j] == str[ii ++])
43                     k ++;
44                 else
45                     break;
46             if(j == len1)
47             {
48                 ans = i + 1;
49                 break;
50             }
51         }
52         printf("%d
",ans);
53     }
54     return 0;
55 }
View Code
悠游天地间 all rights reserved. © 2013 -- 1 << 64
原文地址:https://www.cnblogs.com/jeff-wgc/p/4454561.html