首页 > 试题广场 >

字符串包含

[编程题]字符串包含
  • 热度指数:11161 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
我们定义字符串包含关系:字符串 A=abc ,字符串 B=ab 字符串 C=ac ,则说 包含 B , A 和 C 没有包含关系。

数据范围:输入的字符串长度满足

输入描述:
两个字符串,判断这个两个字符串是否具有包含关系。


输出描述:
如果包含输出1,否则输出0.
示例1

输入

abc ab

输出

1
示例2

输入

abc ac

输出

0
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String str1 = scanner.next();
            String str2 = scanner.next();
            System.out.println((str1.contains(str2) || str2.contains(str1)) ? 1 : 0);
        }
    }
}
编辑于 2019-07-02 11:00:13 回复(1)
#include <iostream>
#include <vector>
using namespace std;
void getNext(string str, vector<int>& next);
int kmp(string str1, string str2);
int main(){

    string str1, str2;
    while(cin >> str1 >> str2){
        if(kmp(str2, str1) != -1 || kmp(str1, str2) != -1 ){
            cout << "1" << endl;
        }else{
            cout << "0" << endl;
        }
    }

    return 0;
}
void getNext(string str, vector<int>& next){
    int n = str.size();
    next[0] = -1;
    next[1] = 0;
    int cn = 0;
    int i = 2;
    while(i < next.size()){
        if(str[i - 1] == str[cn]){
            next[i++] = ++cn;
        }else if(cn > 0){
            cn = next[cn];
        }else{
            next[i++] = 0;
        }
    }
}
int kmp(string str1, string str2){

    int n = str1.size();
    vector<int> next(n, -1);
    getNext(str1, next);
    int i1 = 0, i2 = 0;
    while(i1 < str1.size() && i2 <= str2.size()){
        if(str1[i1] == str2[i2]){
            ++i1, ++i2;
        }else if(i1 == 0){
            ++i2;
        }else{
            i1 = next[i1];
        }
    }
    return i1 == str1.size() ? i2 - i1 : -1;
}
发表于 2019-07-09 19:16:41 回复(0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String str;
        //只要较长的字符串能包含较短的字符串就行
        while ((str = bf.readLine()) != null) {
            String[] s = str.split(" ");
            System.out.println((s[0].length() > s[1].length() ? s[0].contains(s[1]) : s[1].contains(s[0])) ? 1 : 0);
        }
    }
}
发表于 2019-08-05 09:35:20 回复(0)
while True:
    try:
        s1,s2 = input().split()
        print(1 if s1 in s2 or s2 in s1 else 0)
    except:
        break

发表于 2019-06-28 20:45:15 回复(2)
#include <bits/stdc++.h>
using namespace std;
int main(){
    string str1,str2;
    while(cin>>str1>>str2){
        int result=str1.size()>str2.size()?str1.find(str2):str2.find(str1);
        cout<<(result!=string::npos ? 1:0)<<endl;
    }
    return 0;
}


发表于 2019-10-13 21:30:26 回复(0)
#include <bits/stdc++.h>
using namespace std;

int main(){
    string a, b;
    while(cin>>a>>b){
        if(a.find(b)!=-1 || b.find(a)!=-1)
            cout<<1<<endl;
        else
            cout<<0<<endl;
    }
    return 0;
}

发表于 2019-09-27 01:10:39 回复(0)
"""
判断字符串是包含某字符串
"""
import sys

if __name__ == "__main__":
    # sys.stdin = open("input.txt", "r")
    while True:
        try:
            a, b = input().split()
        except:
            break
        print(1 if b in a or a in b else 0)

发表于 2019-07-09 20:37:40 回复(0)
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string A, B;
    while(cin >> A >> B){
        int lenA = A.length();
        int lenB = B.length();
        if(lenA < lenB){
            if(B.find(A) < lenB){
                cout << 1 << endl;
            } else{
                cout << 0 << endl;
            }
        } else{
            if(A.find(B) < lenA){
                cout << 1 << endl;
            } else{
                cout << 0 << endl;
            }
        }
    }
    return 0;
}

发表于 2020-05-25 22:27:34 回复(0)
#include<stdio.h>
(737)#include<string.h>

int main()
{
    char s1[100],s2[100];

    while(scanf("%s %s",s1,s2) != EOF){
        int len1 = strlen(s1);
        int len2 = strlen(s2);
        int max_len = len1>=len2?len1:len2;
        int min_len = len1<=len2?len1:len2;
        int i;

        char *p = len1==max_len?s1:s2;
        char *q = len1==max_len?s2:s1;

        for(i=0;i<=max_len-min_len;i++){

            if((strncmp(p+i,q,min_len)) == 0){
                printf("1\n");
                break;
            }
        }
        if(i > max_len-min_len)
            printf("0\n");

    }
    return 0;
}

发表于 2020-05-01 00:14:57 回复(0)
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()) {
            String in = sc.nextLine();
            System.out.println(isContain(in));
            if(in.contains(" ") && in != null) {
                String[] arr = in.split(" ");
                System.out.println(arr[0].contains(arr[1]) || arr[1].contains(arr[0]) ? 1 : 0);
            }
        }
        sc.close();
	}
}

发表于 2019-09-15 15:34:59 回复(0)
//请大神帮忙看一下,为什么下面的代码不行,多谢!
#include<bits/stdc++.h>

using namespace std;
int main(void) {
	string str1;
	string str2;
	int tmp = -1;
	int a = 0;
	int b = 0;

	while (cin >> str1 >> str2) {
		a = str2.find(str1);
		b = str1.find(str2);
		if(a  >= 0 || b >= 0)
			tmp = 1;

		cout << ((tmp == -1) ? 0 : 1) << endl;
	}

	return 0;
}
编辑于 2019-08-13 22:34:20 回复(1)
while True:
    try:
        strs = list(input().split(' '))
        print(1 if strs[1] in strs[0] or strs[0] in strs[1] else 0)
    except:
        break

发表于 2019-07-06 21:07:04 回复(0)
 #include<iostream>
#include<string>
#include<vector>
using namespace std;
bool judge(string &s, string &t){
    int n = t.length();
    vector<int> next(n,-1);
    for(int i=1; i<n; i++){
        int k = next[i-1];
        while(k!=-1 && t[i-1]!=t[k]) k=next[k];
        next[i] = k+1;
    }
    int j = 0;
    for(int i=0; i<s.length(); i++){
        if(j==t.size()) break;
        if(s[i]==t[j]){
            j++;
        }else{
            j = next[j];
            while(j!=-1 && s[i]!=t[j]) j=next[j];
            j++;
        }
    }
    return j==t.size();
}
int main(){
    string s, t;
    while(cin>>s>>t){
        if(judge(s,t) || judge(t, s)) cout<<1<<endl;
        else cout<<0<<endl;
    }
}

发表于 2019-07-03 11:18:06 回复(0)
package main

import (
    "fmt"
    "strings"
)

func main() {
    var s1,s2 string
    fmt.Scan(&s1,&s2)
    if len(s1)<len(s2){
        s1,s2=s2,s1
    }
    idx:=strings.Index(s1,s2)
    if idx==-1{
        fmt.Print(0)
    }else{
        fmt.Print(1)
    }
}

发表于 2023-03-22 13:40:08 回复(0)
#include<bits/stdc++.h>
using namespace std;
int main()
{
    string a,b;
    cin>>a>>b;
    if(a.find(b)!=a.npos||b.find(a)!=b.npos)    cout<<1<<endl;
    else cout<<0<<endl;
}

发表于 2022-04-10 14:14:44 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        int temp=0;
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()){
             String[] s = in.nextLine().split(" ");
            if(s[0].length()<s[1].length()){
                temp = s[1].indexOf(s[0],0);
            }else{
                temp = s[0].indexOf(s[1],0);
            }
            if(temp<0) 
            System.out.println("0");
            else
            System.out.println("1");
        }
           
    }
}

发表于 2021-04-22 16:03:29 回复(0)
#include<iostream>
#include<string>
int main()
{
    std::string A,B;
    while(std::cin>>A>>B)
    {
        if(A.size()>B.size())swap(A,B);
        if(B.find(A)==B.npos)std::cout<<0<<std::endl;
        else std::cout<<1<<std::endl;
    }
    return 0;
}
发表于 2020-09-09 10:58:32 回复(0)
import sys
in_str = sys.stdin.readlines()
for i in range(len(in_str)):
    str1 = in_str[i].strip().split()[0]
    str2 = in_str[i].strip().split()[1]
    if str2 in str1 or str1 in str2:
        print(1)
    else:
        print(0)
发表于 2020-06-10 17:59:26 回复(0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        while(true) {
        String[] t=br.readLine().split(" ");
        String a=t[0],b=t[1];
        if(a.contains(b))
        {
            System.out.println(1);
        }
        else
        {
            System.out.println(0);
        }
        }
    }
}
谁能告诉我为什么不对?通过率0%
发表于 2020-05-04 22:03:15 回复(2)
import java.util.*;
public class Main{
    private static Scanner sc;
    public static void main(String[] args){
        sc = new Scanner(System.in);
        while(sc.hasNext()){
            String[] strs = sc.nextLine().split(" ");
            String str1 = strs[0];
            String str2 = strs[1];
            if(str1.length() < str2.length()){
                String temp = str1;
                str1 = str2;
                str2 = temp;
            }
            int s1len = str1.length();
            int s2len = str2.length();
            int j = 0;
            for(int i = 0;i<s1len;i++){
                if(str1.charAt(i) != str2.charAt(j)){
                    j = 0;
                    continue;
                }else{
                    j++;
                    if(j == s2len){
                        System.out.println(1);
                        break;
                    }
                }
            }
            if(j < s2len){
                System.out.println(0);
            }
        }
    }
}

发表于 2020-03-01 00:12:12 回复(0)