给出两个字符串s1和s2,判断 s2里面所有的字母s1都有,有的话 返回 true 否则返回 false
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <iostream> #include <set> using namespace std; int main() { string s1, s2; cin >> s1 >> s2; set<char> t1, t2; for(int i = 0; i <= s1.length(); i++) { t1.insert(s1[i]); } for(int i = 0; i <= s2.length(); i++) { t2.insert(s2[i]); } set<char>::iterator it1 = t1.begin(); set<char>::iterator it2 = t2.begin(); while(it1 != t1.end() && it2 != t2.end()) { if(*it1 == *it2) { it2++; } it1++; } if(it2 != t2.end()) { cout << "false"; } else { cout << "true"; } return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> using namespace std; int book[26]; int main() { string s1, s2; cin >> s1 >> s2; for(int i = 0; i < s1.length(); i++) { book[s1[i] - 'A'] = 1; } for(int i = 0; i < s2.length(); i++) { if(book[s2[i] - 'A'] == 0) { cout << "false"; return 0; } } cout << "true"; return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <limits.h> double toSum(char*, int*); int isPrime(int number); void twertySixPrimeNumber(int *numbers); double isContain(double d1, double d2); int main(int arg, char** argvs) { char *s1 = "BAZZY"; char *s2 = "YA"; int primeNumbers[26] = { 0 }; twentySixPrimeNumber(primeNumbers); double d1 = toSum(s1, primeNumbers); double d2 = toSum(s2, primeNumbers); double r = isContain(d1, d2); printf("s1: %s s2:%s \n", s1, s2); if (r) { printf("YES"); } else { printf("NO"); } } double isContain(double d1, double d2) { double a = d1 / d2; double intpart; return 0 == modf(a, &intpart) ? 1 : 0; } double toSum(char *s1, int *numbers) { double sum = 1; for(int i = 0; i < strlen(s1) ; i++ ) { char c = toupper(s1[i]); int temp = c - 65; int primeNumber = numbers[temp]; sum *= primeNumber; } return sum; } void twentySixPrimeNumber(int *numbers) { int counter = 26; for (int i = 2 ; i < INT_MAX; i++) { if (!counter) break; if ( isPrime(i) ) { numbers[26-counter] = i; counter--; } } } int isPrime(int number) { if ( 1 == number || 2 == number || 3 == number) return 1; for (int i = 2; i < sqrt(number); i++) { if (!(number % i)) { return 0;} else { return 1;} } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> int judge(int vector, int vectorS2); int toVector(char*); int main(int arg, char** argvs) { char *s1 = "ACBSDEREJASLKDASD"; char *s2 = "jbd"; int v1 = toVector(s1); int v2 = toVector(s2); int v3 = v1 & v2; if ( 1 == judge(v3, v2) ) { printf("%s is in the %s ", s2, s1); } else { printf("%s was not in the %s ", s2, s1); } } int judge(int vector, int vectorS2) { if (abs((vector - vectorS2)) < 0.1) { return 1; } else { return vector == 0 ? -1 : 0; } } int toVector(char *s1) { int vector = 0; for(int i = 0; i < strlen(s1) ; i++ ) { char c = toupper(s1[i]); int temp = c - 65; vector |= (1 << temp); } return vector; } |
❤ 点击这里 -> 订阅《PAT | 蓝桥 | LeetCode学习路径 & 刷题经验》by 柳婼