D - Cyclic Nacklace(求可以构成循环的最少补充的字符)

本文介绍了一种通过添加最少数量的彩色珍珠到普通手链两端,以制作成具有循环特性的魅力手链的方法。该方法使用非优化的KMP算法来确定最少需要添加多少颗珍珠。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, without any surprise, there are only 99.9 yuan left. he is too distressed and thinking about how to tide over the last days. Being inspired by the entrepreneurial spirit of “HDU CakeMan”, he wants to sell some little things to make money. Of course, this is not an easy task.

As Christmas is around the corner, Boys are busy in choosing christmas presents to send to their girlfriends. It is believed that chain bracelet is a good choice. However, Things are not always so simple, as is known to everyone, girl’s fond of the colorful decoration to make bracelet appears vivid and lively, meanwhile they want to display their mature side as college students. after CC understands the girls demands, he intends to sell the chain bracelet called CharmBracelet. The CharmBracelet is made up with colorful pearls to show girls’ lively, and the most important thing is that it must be connected by a cyclic chain which means the color of pearls are cyclic connected from the left to right. And the cyclic count must be more than one. If you connect the leftmost pearl and the rightmost pearl of such chain, you can make a CharmBracelet. Just like the pictrue below, this CharmBracelet’s cycle is 9 and its cyclic count is 2:
这里写图片描述
Now CC has brought in some ordinary bracelet chains, he wants to buy minimum number of pearls to make CharmBracelets so that he can save more money. but when remaking the bracelet, he can only add color pearls to the left end and right end of the chain, that is to say, adding to the middle is forbidden.
CC is satisfied with his ideas and ask you for help.
Input
The first line of the input is a single integer T ( 0 < T <= 100 ) which means the number of test cases.
Each test case contains only one line describe the original ordinary chain to be remade. Each character in the string stands for one pearl and there are 26 kinds of pearls being described by ‘a’ ~’z’ characters. The length of the string Len: ( 3 <= Len <= 100000 ).
Output
For each case, you are required to output the minimum count of pearls added to make a CharmBracelet.
Sample Input

3
aaa
abca
abcde

Sample Output

0
2
5

求最少在结尾补几个字符才能使得此串形成循环。
要用非优化的kmp来处理。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 100009;
char pattern[maxn];
int nnext[maxn];
void pre()
{
   int patternLen = strlen(pattern);
   int i = 0, j = -1;
   nnext[0] = -1;
   while(i!=patternLen)
   {
      if(j==-1||pattern[i]==pattern[j])
      nnext[++i] = ++j;
      else
      j = nnext[j];
   }
}
int main()
{
  int T;
  scanf("%d", &T);
  while(T--)
  {
    scanf("%s", pattern);
    pre();
    int patternLen = strlen(pattern);
    int n = patternLen - nnext[patternLen];//循环节长度
    if(n!=patternLen&&patternLen%n==0)
    {
      printf("0\n");
    }
    else
    printf("%d\n", n - nnext[patternLen]%n);//取余是去掉可以循环的长度
  }
  return 0;
}
<think>嗯,用户问的是关于SG-cyclic模式下axidma停止循环传输后如何重启通道再次传输的问题。首先,我需要回忆一下SG-cyclic模式是什么。SG代表Scatter-Gather,这是一种DMA传输方式,允许分散的数据块通过链表结构传输,而不需要CPU频繁介入。而cyclic模式可能指的是循环传输,数据在传输完成后自动重新开始,常用于音频或视频流等持续数据传输的场景。 用户提到的axidma可能是指Xilinx的AXI DMA控制器,用于在Zynq SoC或其他FPGA平台上管理DMA传输。AXI DMA支持SG模式,包括循环传输。当用户停止循环传输后,想要再次启动通道,可能需要重新配置DMA控制器,重新初始化描述符链表,或者重新启动DMA通道。 首先,我得确认SG-cyclic模式的具体工作机制。在循环传输中,DMA会不断循环传输描述符链表中的数据块,直到被显式停止。一旦停止,可能需要重新设置描述符的指针,或者重新使能DMA通道。另外,AXI DMA的驱动可能有特定的函数或寄存器操作来完成这些步骤。 可能的步骤包括: 1. 停止当前DMA传输:可能需要禁用DMA通道,等待传输完成或强制停止。 2. 重置DMA通道:确保通道处于空闲状态,清除可能存在的错误状态。 3. 重新配置描述符链表:如果之前的传输已经修改了描述符,或者需要更新数据缓冲区,可能需要重新初始化链表。 4. 重新启动DMA通道:设置控制寄存器,使能循环模式,并启动传输。 需要注意的是,在重启过程中,必须确保描述符链表的正确性,包括每个描述符的next指针指向正确的下一个描述符,最后一个描述符的next指针指向链表头部以实现循环。此外,缓冲区的物理地址需要正确无误,以避免DMA访问错误。 还有可能遇到的问题是,停止DMA后没有正确复位通道,导致无法重新启动。或者描述符链表在传输过程中被修改,导致DMA读取到无效的地址。此外,中断处理也需要正确配置,以处理传输完成或错误的中断。 可能需要查阅Xilinx的官方文档,如AXI DMA的PG021文档,确认具体的寄存器操作和驱动API。例如,在Linux内核中,axidma的驱动可能提供了ioctl命令来启动、停止和配置DMA通道。用户可能需要调用相应的函数,传递必要的参数,如通道号、描述符地址等。 总结来说,处理步骤大致是停止传输,重置通道,重新配置(如果需要),然后再次启动。同时,确保硬件和软件层面的配置正确,避免遗留状态影响重启。</think>在基于Xilinx FPGA的AXI DMA(Direct Memory Access)控制器中,**SG-cyclic模式**(Scatter-Gather循环模式)是一种高效的数据传输方式,适用于需要持续循环传输的场景(如音频/视频流)。当停止传输后需重启通道时,需遵循以下步骤: --- ### **1. 停止循环传输** - **禁用DMA通道**: 通过控制寄存器(如`MM2S_DMACR`/`S2MM_DMACR`)禁用DMA通道,终止当前传输。 示例代码(寄存器操作): ```c dma_ctrl_reg &= ~DMA_CR_RUNSTOP_MASK; // 清除运行位 ``` - **等待空闲状态**: 检查状态寄存器(如`MM2S_DMASR`/`S2MM_DMASR`),确保通道进入`Idle`状态且无错误(如`DMA_INTERNAL_ERROR`)。 --- ### **2. 重置通道状态** - **软复位(可选)**: 若通道存在错误,需执行软复位(设置控制寄存器的`RESET`位),清除残留状态: ```c dma_ctrl_reg |= DMA_CR_RESET_MASK; while (dma_ctrl_reg & DMA_CR_RESET_MASK); // 等待复位完成 ``` - **清除中断标志**: 清除所有挂起的中断(如传输完成中断`IOC_Irq`或错误中断)。 --- ### **3. 重新配置描述符链表** - **描述符链表(Descriptor Chain)**: SG-cyclic模式依赖描述符链表(每个描述符包含数据缓冲区地址、长度及控制信息)。 - **循环链表结构**:最后一个描述符的`NEXT_DESC`指针需指向链表头部。 - **缓冲区对齐**:确保物理地址对齐(通常按32字节或缓存行对齐)。 - **更新数据(可选)**: 若需传输新数据,更新描述符中的缓冲区地址和长度。 --- ### **4. 重启通道** - **设置循环模式**: 在控制寄存器中使能循环模式(如`CYCLIC`位): ```c dma_ctrl_reg |= DMA_CR_CYCLIC_MASK; ``` - **加载描述符地址**: 将链表头部的物理地址写入当前描述符寄存器(如`MM2S_CURDESC`/`S2MM_CURDESC`)。 - **启动通道**: 设置运行位,启动传输: ```c dma_ctrl_reg |= DMA_CR_RUNSTOP_MASK; ``` --- ### **关键注意事项** 1. **内存一致性**: 若使用缓存,需确保缓冲区通过`dma_sync_single_for_device()`同步数据。 2. **中断处理**: 配置中断服务程序(ISR)处理传输完成或错误事件。 3. **硬件约束**: 参考Xilinx文档(PG021)确认DMA IP核版本支持的循环模式细节。 --- ### **示例代码框架(Linux驱动)** ```c // 停止传输 iowrite32(DMA_HALT, dma_base + DMA_CONTROL_REG); // 重置通道 iowrite32(DMA_RESET, dma_base + DMA_CONTROL_REG); while (ioread32(dma_base + DMA_STATUS_REG) & DMA_RESET_STATUS); // 配置描述符链表 setup_descriptors(desc_phys_addr, buffer_phys, buffer_len); // 设置循环模式并启动 iowrite32(desc_phys_addr, dma_base + CUR_DESC_REG); iowrite32(DMA_CYCLIC | DMA_START, dma_base + DMA_CONTROL_REG); ``` 通过以上步骤,可安全重启SG-cyclic模式的AXI DMA传输。具体实现需结合硬件平台和驱动框架调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值