我们定义字符串包含关系:字符串 A=abc ,字符串 B=ab ,字符串 C=ac ,则说 A 包含 B , A 和 C 没有包含关系。
数据范围:输入的字符串长度满足
#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; }
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); } } }
while True: try: s1,s2 = input().split() print(1 if s1 in s2 or s2 in s1 else 0) except: break
#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; }
#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; }
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(); } }
#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; }
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
#include<iostream>
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); } } }
#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; }
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"); } } }