StringProblem(for lab)

来源:互联网 发布:提醒软件reminder 编辑:程序博客网 时间:2024/06/02 07:30

StringProblem(for lab)

标签(空格分隔): 程序设计实验 c++

本人学院

  • StringProblemfor lab
    • 标签空格分隔 程序设计实验 c
    • 本人学院
      • 读题
      • my answer
      • the standard answer


Give you two strings a, b, determine whether string a contains the string b.
Input Format
The first line contains a integer n, represents the number of tests
The next n lines contain the n prolems.Each line contains two strings a , b.

Output Format
For each problem,if A contains B, output”YES” , otherwise output “NO”(without quotes)
Sample Input

2
abcab abc
aabbc ac
Sample Output

YES
NO

Hint:

请使用c++来完成这个题目

读入输出使用cin cout (使用 iostream库)

字符串使用string类型。(使用string库)

判断包含关系使用string的find函数,以及string::npos

读题

string中的字符串查找

my answer

#include<iostream>#include<string>using namespace std;int main() {    int t;    cin >> t;    while (t--) {    string a, b;    cin >> a >> b;    size_t find = a.find(b, 0);    if (find != string::npos)        cout << "YES" << endl;    else        cout << "NO" << endl;    }    return 0;}

the standard answer

#include <iostream>#include <string>using namespace std;int main() {    string a, b;    int t;    cin >> t;    while (t--) {        cin >> a >> b;        if (a.find(b) != string::npos) {            cout << "YES" << endl;        } else {            cout << "NO" << endl;        }    }    return 0;}
0 0
原创粉丝点击