swustoj字符串查找(0448)

来源:互联网 发布:stc89c51单片机烧录 编辑:程序博客网 时间:2024/05/17 22:35

在一段句子中找出给定字符串出现在句子中第一个字母出现的位置。 句子中字符个数小于4500。 字符串字符个数小于120。

Description

两行 第一行是给定字符串 第二行是句子

Input

整数,字符串出现的位置

Output
1
2
abcde
this is a Example of abcde!
Sample Input
1
22
#include<stdio.h>#include<iostream>#include<string.h>#include<math.h>#include<algorithm>#include<string>using namespace std;int main(){char str1[150];char str2[4505];scanf("%[^\n]s", str1);//坑点,害我wrong了3次//第一个字符串也有可能有空格getchar();scanf("%[^\n]s", str2);//cout << str2 << endl;int flag = 0;int ans = 0;for (int i = 0; i < strlen(str2)&&flag == 0; i++){if (str2[i] == str1[0]){int t = 1;int cnt = 0;for (int j = i + 1; t < strlen(str1)&&cnt == 0; j++, t++){if (str1[t] != str2[j]){cnt++;}}if (cnt == 0){ans = i+1;flag = 1;}}}cout << ans << endl;return 0;}


0 0
原创粉丝点击