hdu3336 Count the string(dp+kmp)

决策大模型与生成式大模型有何不同?一文拆解技术框架与落地路径 大模型技术正从内容生成走向复杂场景的智能决策。与生成式大模型拟合概率分布不同,决策大模型以序列决策为核心,融合强化学习机制,在动态环境中感知反馈、持续修正行动路径。其技术框架涵盖语义理解、规划推理与闭环决策,并通过世界模型进行低成本仿真推演,显著提升供应链调度、物流排程等场景的应对效率。实际落地时需结合传统优化算法与人工兜底,构建“语义先导+数值寻优”的分层架构,才能平衡精度与稳定性。这篇内容拆解决策大模型的技术组成、落地路径与常见误区,为企业智能化转型提供可参考的实践框架。 阅读详情

It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s, we can write down all the non-empty prefixes of this string. For example: 
s: "abab" 
The prefixes are: "a", "ab", "aba", "abab" 
For each prefix, we can count the times it matches in s. So we can see that prefix "a" matches twice, "ab" matches twice too, "aba" matches once, and "abab" matches once. Now you are asked to calculate the sum of the match times for all the prefixes. For "abab", it is 2 + 2 + 1 + 1 = 6. 
The answer may be very large, so output the answer mod 10007. 

Input

The first line is a single integer T, indicating the number of test cases. 
For each case, the first line is an integer n (1 <= n <= 200000), which is the length of string s. A line follows giving the string s. The characters in the strings are all lower-case letters. 

Output

For each case, output only one number: the sum of the match times for all the prefixes of s mod 10007.

Sample Input

1
4
abab

Sample Output

6

应该说是一道常规dp+计数转移,最后自己推出来关系式,然而明明写的都和网上搜的答案高度一致了。
自己的程序就是WA,网上的就能AC,最玄学的题...
思路:就是如果能从i转移到next[i],就会在答案中继承next[i]对应的ans部分;
而最长前缀与最长后缀相等,贡献了额外的一个答案。
即核心部分:if(j!=-1)a[i]==a[j]?ans[i]=ans[j]+1:0;

自己的代码(WA):

//将s1串循环接到自己身上,一遍即可 
/*abcdabc next[6]=3时 a=a b=b c=c ab=ab bc=bc abc=abc 6 即ans+=3+2+1  
aaaabaaaa
next[0]=-1 ans[0]=0
next[1]=0 ans[1]=0
next[2]=1 ans[2]=1
next[3]=2 ans[3]=1,ans[3]+=ans[2]=2
next[4]=3 ans[4]=1,ans[4]+=ans[3]=3
next[5]=0 ans[5]=0
123=234 ans[4]=1
12=23 ans[3]=1

能推出23=34即12=34 是一组新解
*/
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int mod=10007;
char t[200005];
int net[200005],ans[200005];
int kmppre(char s[],int m)
{
	int i,j=net[0]=-1,res=0;
	while(i<m)
	{
		if(s[i]==s[j])
		{
		ans[i]=ans[j]+1;
		res+=ans[i];
		if(res>=mod)res%=mod;
	    net[++i]=++j; 
	    }
	    else if(j==-1)net[++i]=++j;
	    else j=net[j];
	}
	return (res+m)%mod;
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		memset(ans,0,sizeof(ans));
	    int n;
	    scanf("%d%s",&n,t);
		printf("%d\n",kmppre(t,n));
    }
	return 0;
} 

代码来自:

https://blog.csdn.net/u011686226/article/details/22044935

https://www.cnblogs.com/yym2013/p/3550775.html

AC代码①:

#include <iostream>
#include <string.h>
using namespace std;
char s[200001];
int nnext[200001];
int c[200001];
int ans;
void GetNext(char t[],int nnext[])
 {
    memset(c,0,sizeof(c));
   int j,k;
     j=0;k=-1;nnext[0] = -1;
     int length;
     for(length = 0;t[length]!='\0';length++);
     while(j<length){
         if(k==-1 || t[j]==t[k]){
             if(k!=-1){
                 c[j] = c[k] + 1;
                 ans+=c[j];    
             }
             j++;k++;
             nnext[j] = k;
         }
         else
             k = nnext[k];
     }
 }
 int main()
 {
     int T,n;
     cin>>T;
     while(T--){         cin>>n;
         cin>>s;
         ans = 0;
         GetNext(s,nnext);
         cout<<(ans+n)%10007<<endl;
     }
     return 0;
 }

AC代码②:

//注意f[1]=0,dp[0]=0的操作
//分f和dp两步进行操作,操作较为常规

#include <cstdio>
#include <cstring>
const int maxn = 200010;
const int mod = 10007;
char a[maxn];
int f[maxn];
int dp[maxn];
int n, m;
 
void getFail()
{
	f[0] = f[1] = 0;
	for(int i = 1; i < n; i++)
	{
		int j = f[i];
		while(j && a[i] != a[j])
			j = f[j];
		f[i+1] = a[i] == a[j] ? j+1 : 0;
	}
	dp[0] = 0;
	for(int i = 1; i <= n; i++)
	{
		dp[i] = dp[f[i]]+1;
		m += dp[i];
		m %= mod;
	}
}
int main()
{
	int T;
	scanf("%d", &T);
	while(T--)
	{
		m = 0;
		scanf("%d %s", &n, a);
		getFail();
		printf("%d\n", m);
	}
	return 0;
}

 

编写一个程序实现顺序表的各种基本运算(假设顺序表的元数基本类型为Char) /* *Copyright (c) 2016, 烟台大学计算机学院 *All rights reserved. *文件名称:main.cpp *作者:张旺华 *完成日期: 2016 年 7 月 1 日 *版本号:v1.0 *问题描述:编写一个程序实现顺序表的各种基本运算(假设顺序表的元数基本类型为Char) * */ #include #include #include 阅读详情

相关推荐

Qwen 三代进化全景对比:从 Qwen2.5 到 Qwen3 再到 Qwen3.5

本文对比了阿里巴巴通义千问三代大模型(Qwen2.5、Qwen3、Qwen3.5)的技术演进。18个月内,模型在架构、训练数据和能力上实现显著突破:从纯稠密结构(Qwen2.5)发展为混合注意力架构(Qwen3.5),支持语言从29种扩展至201种,并实现原生多模态融合。关键进展包括引入MoE架构、Gated DeltaNet混合注意力机制(计算效率提升10倍)、以及从独立模态到早期视觉语言联合训练的转变。模型阵容从7个通用尺寸发展为8+个混合架构,最大参数量达397B,上下文窗口扩展至256K(托管版1M

m0_47999117的博客 1万+

hdu 3336 Count the string KMP+DP

题目链接 Problem Description It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s, we can write down all the non-empty prefixes

读不懂的流年 524

mmpretrain学习笔记

(4)数据集路径 data_root=‘data/imagenet’–》data_root=‘data/Fruits Dataset/images’(7)适当的修改num_workers、epochs、batch_size、lr等。3、训练优化 :梯度下降算法、学习率参数、训练总轮次、学习率变化策略等。my_resnet18_8xb32_in1k.py 完整的配置文件。(6)split=‘train’、split='val’注释掉。2、数据:数据集划分、数据文件路径、批大小、数据增强策略等。

吃葡萄不吐葡萄皮 937

[dp][KMP][hdu3336]Count the string

Count the stringProblem Description It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s, we can write down all the non-empty prefixes of

Freddddd 332

在win10系统上安装opnet14.5注意事项和步聚

opnet 14.5安装避坑!

miracle_eicont的专栏 1046

MAX30102心率血氧传感器:从原理到实践的可穿戴健康监测方案

本文深入解析MAX30102心率血氧传感器的工作原理及在可穿戴健康监测中的实践应用。通过反射式光电容积脉搏波(PPG)技术,该传感器能精准测量心率和血氧饱和度,具备低功耗、高抗干扰等优势。文章详细介绍了信号处理算法、硬件连接技巧及创新应用场景,为开发者提供全面的解决方案。

weixin_29184371的博客 287

HDU 3336 Count the string (kmp+dp)

Count the string Problem Description It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s, we can write down all the non-empty prefixes

Follow My Heart 364

具身智能落地实战:制造业、物流与电力的工业级方案设计

具身智能是一种融合感知、推理与物理执行的智能范式,其核心在于构建具备因果建模能力、多模态闭环反馈和实时动作规划的行动主体。区别于通用AI或传统自动化,它依托多模态大模型、高精度力觉/触觉传感及Sim2Real仿真技术,实现对真实物理环境的深度理解与自适应交互。技术价值体现在可解释性决策、毫秒级跨模态协同与零停机容错能力,已规模化应用于制造业质检、物流破损鉴定、电力绝缘子巡检等强约束工业场景。本文聚焦‘行业应用方案解决方案’这一高频搜索需求,系统拆解从工位筛选、分层架构设计到72小时现场交付的全链路工程方法论

weixin_30952479的博客 309

[HDU 3336]Count the String[kmp][DP]

题意: 求一个字符串的所有前缀串的匹配次数之和. 思路: 首先仔细思考: 前缀串匹配. n个位置, 以每一个位置为结尾, 就可以得到对应的一个前缀串. 对于一个前缀串, 我们需要计算它的匹配次数. k = next [ j ] 表示前缀串 Sj 的范围内(可以视为较小规模的子问题), 前缀串 Sk 是最长的&能够匹配两次的前缀串. 这和我们需要的答案有什么关系呢? 题目是...

weixin_30861459的博客 76

Intouch间接变量(扩展)

间接变量只是Intouch提供给开发者的便捷方式之一,并不是所有情况都一定要使用间接变量,有时候用条件语句或者内存变量的方式也能起到类似的作用。

weixin_41571357的博客 2181

22、云数据中心网络(DCN)部署最佳实践

本文详细介绍了云数据中心网络(DCN)部署的最佳实践,涵盖SDN设计、服务网络规划、管理网络规划等方面。通过合理的架构设计、路由规划和组件协同,提升数据中心的自动化水平、可靠性及安全性,以满足不断增长的业务需求。

d6e7f8的博客 190

HDU - 3336 Count the string dp+kmp

题目链接:点击查看 It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s, we can write down all the non-empty prefixes of this string. For example...

还我笑颜 209

Android Camera MTK框架

Camera简介: 虽然前文说道Android在5.0之后就推出了Camera2 API,但是各个厂家的基本未适配,导致目前市场上大部分机型使用的仍然是Camera1 API。PS:Android 9.0 Google强制要求各个平台厂商(高通/MTK/华为等)支持Camera2,不再兼容Camera1。 ...

weixin_49303682的博客 5519

HDU-3336 Count the string(KMP+DP)

加油。 To be better. 题目链接: https://cn.vjudge.net/problem/HDU-3336 题目: It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s, we can write do...

A's World 655

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

(HDU)3336 - Count the string 【KMP】+【DP】

DP是一门玄学。

Doge 628

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

HDU - 3336 Count the string kmp+dp

题目链接:https://cn.vjudge.net/problem/HDU-3336 题意:字符串所有前缀的出现次数 题解:dp[ i ] 表示以i为结尾i 前缀的个数 ,所以dp[i] = dp[nex[i] ] + 1 #include <bits/stdc++.h> using namespace std; const int N = 200100; const ...

还我笑颜 230

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

HDU 3336 Count the string (KMP+DP)

It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s, we can write down all the non-empty prefixes of this string. For example: s: “abab” The prefixes are: “a”, “ab”, “aba”, “abab” For each pre

weixin_52278699的博客 208

hdu 3336 Count the string (kmp + dp)

http://acm.hdu.edu.cn/showproblem.php?pid=3336 题意:给出一个长度为n的字符串,求出该字符串的所有前缀出现的总次数 思路:kmp求出该字符串的next数组,第i个位置dp[i]可有dp[next[i]]+1得到 #include #include #include #include #include using namespace s

大太阳 782

HDU3336 Count the string(KMP+DP)

计算字符串的每个前缀在字符串里出现的次数。 一个KMP+DP,在KMP的next数组中,next[i]=j就表示在前i个字符串当中,从前1到j的j长度字符字串和i-j+1到i的j长度的字符字串相等。那么,每一个位置的字符只要继承它的next[i]位置的数量,再加一就是它当前的数量了。 #include #include #include using namespace std; cons

HOrchard 254

HDU 3336 Count the string 后缀数组 或 (KMP + DP)

题目大意: 就是现在给出一个长度不超过20W的字符串S, 对于这个字符串求其所有前缀在串中出现次数的和, 结果对10007取模 大致思路: 很容易想到后缀数组, 找到sa[i] = 0的那个就是串S, 那么找出这个位置向两边能扩展到的长度即可, 就是利用一下height数组就行了, 没什么难度= = 还是不清楚的话看代码细节吧 代码如下: Result  :  Acce

Gatevin的专栏 925

HDU 3336-Count the string(kmp+DP)

Problem Description It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s, we can write down all the non-empty prefixes of this string. F...

PleasantlY1的博客 370

HDU 3336 Count the string kmp+dp

点击打开链接 Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3916    Accepted Submission(s): 1834 Problem Descript

代码改变世界,keep coding。 1235

HDU 3336 Count the string kmp 递推dp

题意: 每个前缀出现次数的和. 思路: cnt[pos]记录第pos个前缀x在比他长的前缀y中出现的次数, 递推式: 定义原串的前缀为x类串. 每个长度为a的x类串s会包含2个长度为nxt[a]的x类串t, 而两个相邻的s会有一个重复的t, 所以cnt[a]个s会有cnt[a]+1个t. 最后答案加上每个前缀本身共m. 具体见代码. #include <iostream>...

swqeaaa的博客 209

hdu3336 Count the string kmp+dp

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 很容易想到用kmp 这里是next数组的应用 定义dp[i]表示以s[i]结尾的前缀的总数 那么dp[i]=dp[next[i]]+1; 代码: 1 #include<stdio.h> 2 #include<string.h> 3 const i...

weixin_30821731的博客 160

HDU3336Count the string(dp+kmp_next表性质)

题目链接:hdu3336 题目大意:给一串n长度的字符串str,问str所有前缀(包括本串)在str中的匹配次数。 解题思路:利用kmp爆搜得到一发TLE,参考kmp+dp 以及dp状态方程解释博客思路学会利用next数组的性质进行dp优化:next[i]表示不包含下标为i的字符(i下标之前的串)的最长前后缀相等长度,那么我们设dp[i]表示以第i个字符为结尾 (或者说长度为i)的前缀能够匹配到的...

小胡同的诗 346

Hdu-3336 Count the string(KMP + DP)

Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7697    Accepted Submission(s): 3581 Problem Description It is well kn

WZJRJ28 490
上一篇: hdu6470 Count(矩阵快速幂)
下一篇: poj3281 Dining(网络流/拆点)
小衣同学
博客等级 码龄9年 1444粉丝 995原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小衣同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值