两个整数字符串。检查第二个是否是子串
two strings of integers. Check if the second one is substring
#include <stdio.h>
int main() {
int x[100], y[100], i, j, dim1, dim2, subsir=0;
scanf("%d", &dim1);
for(i=0; i<dim1; i++)
{
scanf("%d", &x[i]);
}
scanf("%d", &dim2);
for(j=0; j<dim2; j++)
{
scanf("%d", &y[j]);
}
if(dim1<dim2)
{
printf("no");
return 0;
}
for(i=0; i<dim1; i++)
for(j=0; j<dim2; j++)
{
if(x[i]==y[j])
subsir=1;
}
if(subsir==1)
{
printf("yes");
}
else
{
printf("no");
}
return 0;
}
考虑两个整数字符串。检查第二个字符串是否是C中第一个字符串的子字符串。(子字符串由第一个字符串的连续元素组成)
基于您的方法的可能实现:
// this function will return `1` in case `arr2` is contained inside `arr1`, otherwise `0`
int is_subarr(int arr1[], size_t arr1_len, int arr2[], size_t arr2_len) {
// as you already did, we first check that arr1's len is not smaller than arr2's
if (arr1_len < arr2_len) {
return 0;
}
// useful check: in many implementations, when `arr2` is empty, they return true
if (arr2_len == 0) {
return 1;
}
// first loop: we go through all elements of `arr1` (but we stop when we have less elements left than `arr2`'s len)
for (size_t i = 0; i < arr1_len && i < arr2_len; i += 1) {
// temporary flag, we just pretend that `arr2` is contained, and then we check for the opposite
int eq = 1;
// starting with the current position `i` inside `arr1`, we start checking if `arr2` is contained
for (size_t j = 0; j < arr2_len; j += 1) {
// if it's not contained, we break from this inner loop, and we keep searching through `arr1`
if (arr1[i + j] != arr2[j]) {
eq = 0;
break;
}
}
// in case `arr2` is effectively found, we return `1`
if (eq == 1) {
return 1;
}
}
return 0;
}
注意:“整数字符串”在 C 中称为“整数数组”;相反,“字符串”是“字符数组”。
您可以通过蛮力方法实现您的目标,尝试从 0
到 dim1 - dim2
的每个位置。
这是一个带有错误检查的修改版本:
#include <stdio.h>
int main() {
int x[100], y[100], i, j, dim1, dim2;
if (scanf("%d", &dim1) != 1 || dim1 > 100)
return 1;
for (i = 0; i < dim1; i++) {
if (scanf("%d", &x[i]) != 1)
return 1;
}
if (scanf("%d", &dim2) != 1 || dim2 > 100)
return 1;
/* no need to read second array if larger */
if (dim1 < dim2) {
printf("no\n");
return 0;
}
for (j = 0; j < dim2; j++) {
if (scanf("%d", &y[j]) != 1)
return 1;
}
/* trying every possible sub-array start in array `x` */
for (i = 0; i <= dim1 - dim2; i++) {
/* compare all entries */
for (j = 0; j < dim2; j++) {
if (x[i + j] != y[j])
break;
}
/* if above loop completed, we have a match */
if (j == dim2) {
printf("yes\n");
return 0;
}
}
printf("no\n");
return 0;
}
#include <stdio.h>
int main() {
int x[100], y[100], i, j, dim1, dim2, subsir=0;
scanf("%d", &dim1);
for(i=0; i<dim1; i++)
{
scanf("%d", &x[i]);
}
scanf("%d", &dim2);
for(j=0; j<dim2; j++)
{
scanf("%d", &y[j]);
}
if(dim1<dim2)
{
printf("no");
return 0;
}
for(i=0; i<dim1; i++)
for(j=0; j<dim2; j++)
{
if(x[i]==y[j])
subsir=1;
}
if(subsir==1)
{
printf("yes");
}
else
{
printf("no");
}
return 0;
}
考虑两个整数字符串。检查第二个字符串是否是C中第一个字符串的子字符串。(子字符串由第一个字符串的连续元素组成)
基于您的方法的可能实现:
// this function will return `1` in case `arr2` is contained inside `arr1`, otherwise `0`
int is_subarr(int arr1[], size_t arr1_len, int arr2[], size_t arr2_len) {
// as you already did, we first check that arr1's len is not smaller than arr2's
if (arr1_len < arr2_len) {
return 0;
}
// useful check: in many implementations, when `arr2` is empty, they return true
if (arr2_len == 0) {
return 1;
}
// first loop: we go through all elements of `arr1` (but we stop when we have less elements left than `arr2`'s len)
for (size_t i = 0; i < arr1_len && i < arr2_len; i += 1) {
// temporary flag, we just pretend that `arr2` is contained, and then we check for the opposite
int eq = 1;
// starting with the current position `i` inside `arr1`, we start checking if `arr2` is contained
for (size_t j = 0; j < arr2_len; j += 1) {
// if it's not contained, we break from this inner loop, and we keep searching through `arr1`
if (arr1[i + j] != arr2[j]) {
eq = 0;
break;
}
}
// in case `arr2` is effectively found, we return `1`
if (eq == 1) {
return 1;
}
}
return 0;
}
注意:“整数字符串”在 C 中称为“整数数组”;相反,“字符串”是“字符数组”。
您可以通过蛮力方法实现您的目标,尝试从 0
到 dim1 - dim2
的每个位置。
这是一个带有错误检查的修改版本:
#include <stdio.h>
int main() {
int x[100], y[100], i, j, dim1, dim2;
if (scanf("%d", &dim1) != 1 || dim1 > 100)
return 1;
for (i = 0; i < dim1; i++) {
if (scanf("%d", &x[i]) != 1)
return 1;
}
if (scanf("%d", &dim2) != 1 || dim2 > 100)
return 1;
/* no need to read second array if larger */
if (dim1 < dim2) {
printf("no\n");
return 0;
}
for (j = 0; j < dim2; j++) {
if (scanf("%d", &y[j]) != 1)
return 1;
}
/* trying every possible sub-array start in array `x` */
for (i = 0; i <= dim1 - dim2; i++) {
/* compare all entries */
for (j = 0; j < dim2; j++) {
if (x[i + j] != y[j])
break;
}
/* if above loop completed, we have a match */
if (j == dim2) {
printf("yes\n");
return 0;
}
}
printf("no\n");
return 0;
}