当前位置: 首页 > news >正文

做游戏网站要通过什么审核陕西省诚信建设示范网这个网站

做游戏网站要通过什么审核,陕西省诚信建设示范网这个网站,企业年金办法,wordpress缩略图传不上2025B卷目录点击查看: 华为OD机试2025B卷真题题库目录|机考题库 算法考点详解 2025B卷 100分题型 题目描述 给定一个由若干整数组成的数组nums,请检查数组是否是由某个子数组重复循环拼接而成,请输出这个最小的子数组。 输入描…

2025B卷目录点击查看: 华为OD机试2025B卷真题题库目录|机考题库 + 算法考点详解

2025B卷 100分题型

题目描述

给定一个由若干整数组成的数组nums,请检查数组是否是由某个子数组重复循环拼接而成,请输出这个最小的子数组。

输入描述

第一行输入数组中元素个数n,1 ≤ n ≤ 100000

第二行输入数组的数字序列nums,以空格分割,0 ≤ nums[i] < 10

备注

数组本身是其最大的子数组,循环1次可生成的自身;

输出描述

输出最小的子数组的数字序列,以空格分割;

示例1

输入

9
1 2 1 1 2 1 1 2 1

输出

1 2 1

说明

数组[1,2,1,1,2,1,1,2,1] 可由子数组[1,2,1]重复循环3次拼接而成

解题思路

问题理解

这道题要求我们找到一个最小的子数组,该子数组通过循环拼接可以构成原始数组。例如,对于数组 [1,2,1,1,2,1,1,2,1],最小的循环子数组是 [1,2,1],它重复3次可以生成原数组。

算法原理

这个问题可以使用 KMP 算法的思想来解决。KMP 算法主要用于字符串匹配,但其中的"前缀函数"(next数组)可以帮助我们找到循环模式。

KMP 的 next 数组在这里的应用

  1. next数组记录了每个位置的最长相等前后缀长度
  2. 对于数组末尾位置 i,next[i] 表示数组的前 next[i] 个元素与末尾的 next[i] 个元素相同
  3. 如果数组是由某个子数组循环构成的,那么 (数组长度 - next[末尾位置]) 就是可能的循环子数组的长度

关键思路

如果一个数组是循环构成的,假设循环子数组长度为 k,数组长度为 n,则有:

  • n 必须是 k 的整数倍
  • 如果 (n % (n - next[n-1]) == 0),说明数组可以被分成整数个循环子数组
  • 此时最小循环子数组长度为 (n - next[n-1])
  • 否则,无法找到真正的循环子数组,整个数组本身就是最小的"循环子数组"(循环1次)

示例分析

对于输入 [1,2,1,1,2,1,1,2,1]

  1. 构建 next 数组:[0,0,1,1,2,3,4,5,6]
  2. 最后一个元素的 next 值是 6
  3. 计算:9 % (9 - 6) = 9 % 3 = 0,可以整除
  4. 因此最小循环子数组长度为 3
  5. 输出前 3 个元素:[1,2,1]

Java

import java.util.Arrays;
import java.util.Scanner;
import java.util.StringJoiner;public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);// 读取数组长度int arrayLength = Integer.parseInt(sc.nextLine());// 读取数组元素并将其转换为整数数组int[] nums = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();// 初始化next数组,用于存储KMP算法中的前缀信息int[] next = new int[arrayLength];// 初始化前缀索引int prefixIndex = 0;// 初始化当前索引int currentIndex = 1;// 使用KMP算法的思想构建next数组while (currentIndex < arrayLength) {// 如果当前元素与前缀元素相等,则更新next数组if (nums[currentIndex] == nums[prefixIndex]) {next[currentIndex++] = ++prefixIndex;} else if (prefixIndex > 0) {// 如果前缀索引大于0,则回退到前一个前缀prefixIndex = next[prefixIndex - 1];} else {// 如果前缀索引为0,则移动到下一个元素currentIndex++;}}// 获取next数组的最后一个值int lastNextValue = next[arrayLength - 1];// 计算子数组长度int subArrayLength = arrayLength % (arrayLength - lastNextValue) == 0 ? arrayLength - lastNextValue : arrayLength;// 使用StringJoiner构建输出字符串StringJoiner sj = new StringJoiner(" ");// 将子数组的元素添加到输出字符串中for (int i = 0; i < subArrayLength; i++) sj.add(nums[i] + "");// 输出结果System.out.println(sj.toString());}
}

Python

import sys# 从控制台读取输入
input_lines = sys.stdin.readlines()
# 读取数组长度
array_length = int(input_lines[0].strip())
# 读取数组元素并将其转换为整数数组
nums = list(map(int, input_lines[1].strip().split()))# 初始化next数组,用于存储KMP算法中的前缀信息
next = [0] * array_length
# 初始化前缀索引
prefix_index = 0
# 初始化当前索引
current_index = 1# 使用KMP算法的思想构建next数组
while current_index < array_length:# 如果当前元素与前缀元素相等,则更新next数组if nums[current_index] == nums[prefix_index]:prefix_index += 1next[current_index] = prefix_indexcurrent_index += 1elif prefix_index > 0:# 如果前缀索引大于0,则回退到前一个前缀prefix_index = next[prefix_index - 1]else:# 如果前缀索引为0,则移动到下一个元素current_index += 1# 获取next数组的最后一个值
last_next_value = next[array_length - 1]
# 计算子数组长度
sub_array_length = array_length - last_next_value if array_length % (array_length - last_next_value) == 0 else array_length# 使用列表构建输出字符串
output = ' '.join(str(nums[i]) for i in range(sub_array_length))
# 输出结果
print(output)

JavaScript

const readline = require('readline');const rl = readline.createInterface({input: process.stdin,output: process.stdout
});// 读取用户输入
rl.on('line', arrayLength => {rl.on('line', arrayElements => {// 将输入的字符串转换为整数数组const nums = arrayElements.split(' ').map(Number);const next = new Array(parseInt(arrayLength)).fill(0);let prefixIndex = 0;let currentIndex = 1;// 使用KMP算法的思想构建next数组while (currentIndex < nums.length) {if (nums[currentIndex] === nums[prefixIndex]) {next[currentIndex++] = ++prefixIndex;} else if (prefixIndex > 0) {prefixIndex = next[prefixIndex - 1];} else {currentIndex++;}}// 获取next数组的最后一个值const lastNextValue = next[nums.length - 1];// 计算子数组长度const subArrayLength = nums.length % (nums.length - lastNextValue) === 0 ? nums.length - lastNextValue : nums.length;// 构建输出字符串const output = nums.slice(0, subArrayLength).join(' ');console.log(output);rl.close();});
}) 

C++

#include <iostream>
#include <vector>
#include <sstream>
#include <string>
using namespace std;
int main() {// 创建输入流对象以读取用户输入string line;// 读取数组长度int arrayLength;getline(cin, line);arrayLength = stoi(line);// 读取数组元素并将其转换为整数数组getline(cin, line);istringstream iss(line);vector<int> nums(arrayLength);for (int i = 0; i < arrayLength; ++i) {iss >> nums[i];}// 初始化next数组,用于存储KMP算法中的前缀信息vector<int> next(arrayLength, 0);// 初始化前缀索引int prefixIndex = 0;// 初始化当前索引int currentIndex = 1;// 使用KMP算法的思想构建next数组while (currentIndex < arrayLength) {// 如果当前元素与前缀元素相等,则更新next数组if (nums[currentIndex] == nums[prefixIndex]) {next[currentIndex++] = ++prefixIndex;} else if (prefixIndex > 0) {// 如果前缀索引大于0,则回退到前一个前缀prefixIndex = next[prefixIndex - 1];} else {// 如果前缀索引为0,则移动到下一个元素currentIndex++;}}// 获取next数组的最后一个值int lastNextValue = next[arrayLength - 1];// 计算子数组长度int subArrayLength = arrayLength % (arrayLength - lastNextValue) == 0 ? arrayLength - lastNextValue : arrayLength;// 使用ostringstream构建输出字符串ostringstream oss;// 将子数组的元素添加到输出字符串中for (int i = 0; i < subArrayLength; i++) {oss << nums[i];if (i < subArrayLength - 1) {oss << " ";}}// 输出结果cout << oss.str() << endl;return 0;
}

C语言

#include <stdio.h>
#include <stdlib.h>
#include <string.h>int main() {// 读取数组长度int arrayLength;scanf("%d", &arrayLength);// 读取数组元素int* nums = (int*)malloc(arrayLength * sizeof(int));for (int i = 0; i < arrayLength; i++) {scanf("%d", &nums[i]);}// 初始化next数组,用于存储KMP算法中的前缀信息int* next = (int*)calloc(arrayLength, sizeof(int));// 初始化前缀索引int prefixIndex = 0;// 初始化当前索引int currentIndex = 1;// 使用KMP算法的思想构建next数组while (currentIndex < arrayLength) {// 如果当前元素与前缀元素相等,则更新next数组if (nums[currentIndex] == nums[prefixIndex]) {prefixIndex++;next[currentIndex] = prefixIndex;currentIndex++;} else if (prefixIndex > 0) {// 如果前缀索引大于0,则回退到前一个前缀prefixIndex = next[prefixIndex - 1];} else {// 如果前缀索引为0,则移动到下一个元素next[currentIndex] = 0;currentIndex++;}}// 获取next数组的最后一个值int lastNextValue = next[arrayLength - 1];// 计算子数组长度int subArrayLength = arrayLength % (arrayLength - lastNextValue) == 0 ? arrayLength - lastNextValue : arrayLength;// 输出结果for (int i = 0; i < subArrayLength; i++) {printf("%d", nums[i]);if (i < subArrayLength - 1) {printf(" ");}}printf("\n");// 释放内存free(nums);free(next);return 0;
}
http://www.dtcms.com/a/531217.html

相关文章:

  • 丽水专业做网站自己做培训网站
  • 网站搭建合同html网页制作期末作业
  • 免费网站设计软件清远建设局网站
  • 宁波网站建设优化企业简历模板制作
  • 网站建站主题动漫网站设计
  • 网站底部模板源码金蝶网站建设公司案例
  • 网站dns解析设置做外贸网站那个好
  • 网站建设3000字盐城集团网站建设
  • 门户网站改造方案怎么简单页网站
  • 北京城市雕塑建设管理办公室网站wordpress架设主机
  • 超值的网站建设哪里有html5网站建设
  • 在线包车网站建设长沙微网站电话号码
  • 广州手机网站建设公司哪家好开发什么软件有市场
  • 如何用h5做网站芜湖做公司网站
  • 指定网站怎么设置路由器只访问合肥建设工程交易网站
  • 国内网站设计制作长安镇做网站
  • 网站定制电话贴吧网站建设
  • 国防教育网站建设方案文昌市建设局网站
  • 南京网站制作报价微信小程序注册要钱吗
  • 免费建站工具wordpress安装linux
  • 什么是网站推广?线上网站建设需求
  • 流放之路做装备词缀网站seo排名优化推荐
  • nginx 做网站色彩搭配比较好的网站
  • 网站做跳转在后天那个文件里做网站icp备案费用
  • 东莞网站建没刚做的网站 搜不到
  • 即墨区建设局网站外国网站怎么进入
  • 企业网站备案注销太原做网站设计
  • 广州华优_网站建设公司0基础学网站开发
  • 中英文网站如何做思路如何建设淘宝客网站
  • 温州多语言网站建设仿站网站