51Nod1553 周期串查询

该博客探讨了如何处理周期串查询问题,通过利用border的性质,确定了周期串的充分必要条件,并提出使用线段树和哈希数据结构进行动态维护的方法。

题目看这里

我们发现需要动态维护一个字符串是否成周期

根据border的一个简单性质,得出周期串的充分必要条件是,如果a[i..k]=a[j-k+1..j] 那么a[i..j]是以k为周期的串

于是可以用线段树来维护哈希

莫名其妙rank1..

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N 100010
#define LL long long 
#define mid (l+r>>1)
#define ls x<<1
#define rs x<<1|1
using namespace std;
int n,m,k,s[N<<2]; char c[N];
LL bas[N],p[10][N],w[N<<2];
inline void ps(int l,int r,int x){
    w[x]=w[ls]*bas[r-l+1>>1]+w[rs];
}
inline void pd(int x,int m){
    if(s[x]){
        s[ls]=s[rs]=s[x];
        w[ls]=p[s[x]-'0'][m+1>>1];
        w[rs]=p[s[x]-'0'][m>>1];
        s[x]=0;
    }
}
inline void build(int l,int r,int x){
    if(l==r){ w[x]=c[l]-'0'; return; }
    build(l,mid,ls);
    build(mid+1,r,rs);
    ps(l,r,x);
}
inline void update(int l,int r,int x,int L,int R,int k){
    if(L<=l && r<=R){ w[x]=p[k-'0'][r-l+1]; s[x]=k; return; }
    pd(x,r-l+1);
    if(L<=mid) update(l,mid,ls,L,R,k);
    if(mid<R) update(mid+1,r,rs,L,R,k);
    ps(l,r,x);
}
inline LL gH(int l,int r,int x,int L,int R){
    if(L<=l && r<=R) return w[x];
    pd(x,r-l+1); LL S=0;
    if(L<=mid) S=gH(l,mid,ls,L,R);
    if(mid<R) S=S*bas[min(r,R)-mid]+gH(mid+1,r,rs,L,R);
    return S;
}
int main(){
    scanf("%d%d%d%s",&n,&m,&k,c+1); m+=k;
    for(int i=*bas=1;i<=n;++i){
        bas[i]=bas[i-1]*13;
        for(int j=0;j<10;++j)
            p[j][i]=p[j][i-1]*13+j;
    }
    build(1,n,1);
    for(int o,l,r,c;m--;){
        scanf("%d%d%d%d",&o,&l,&r,&c);
        if(o==1) update(1,n,1,l,r,c+'0');
        else {
            if(c==r-l+1) puts("YES");
            else if(gH(1,n,1,l,r-c)==gH(1,n,1,l+c,r)) puts("YES"); else puts("NO");
        }
    }
}

// 修复动作识别逻辑(基于关键点计算,更可靠) analyzeAction(result) { const self = this; const currentAction = this.selectedActions[this.verificationStep - 1]; if (!currentAction) return; const landmarks = result.landmarks; if (!landmarks) { self.currentPrompt = '未检测到面部特征,请对准摄像头'; self.promptStatus = 'warning'; return; } // 提取必要的面部特征点集 const facePoints = landmarks.positions; if (!facePoints || facePoints.length < 68) { self.currentPrompt = '面部特征点不足,请调整姿势'; self.promptStatus = 'warning'; return; } // 计算面部中心位置 const faceCenter = { x: (facePoints[0].x + facePoints[16].x) / 2, y: (facePoints[27].y + facePoints[8].y) / 2 }; // 计算面部宽度和高度作为参考 const faceWidth = Math.abs(facePoints[16].x - facePoints[0].x); const faceHeight = Math.abs(facePoints[8].y - facePoints[27].y); // 自适应调整检测阈值(根据面部大小动态调整) const adaptiveThreshold = { nod: Math.max(6, faceHeight * 0.12), // 点头阈值(基于面部高度百分比) shake: Math.max(8, faceWidth * 0.15), // 摇头阈值 eye: Math.max(3, faceHeight * 0.05), // 眨眼阈值 mouth: Math.max(4, faceWidth * 0.1) // 张嘴阈值 }; // 1. 点头检测(基于鼻尖和下巴的相对位置变化) if (currentAction.type === 'nod') { // 提取关键特征点 const forehead = (facePoints[19].y + facePoints[24].y) / 2; // 额头平均y坐标 const eyeLine = (facePoints[36].y + facePoints[45].y) / 2; // 眼线平均y坐标 const jawLine = (facePoints[1].y + facePoints[17].y + facePoints[8].y) / 3; // 下颌平均y坐标 // 计算头部俯仰角(基于三点连线斜率) const upperDist = Math.abs(eyeLine - forehead); // 额头到眼线的垂直距离 const lowerDist = Math.abs(jawLine - eyeLine); // 眼线到下颌的垂直距离 const pitchRatio = upperDist / lowerDist; // 俯仰比(低头时upperDist增大,pitchRatio上升) // 初始化基准(取前5帧的pitchRatio均值作为静止基准) if (!self.nodBaseRatio) { self.nodRatioHistory = self.nodRatioHistory || []; self.nodRatioHistory.push(pitchRatio); if (self.nodRatioHistory.length >= 5) { self.nodBaseRatio = self.nodRatioHistory.reduce((a, b) => a + b, 0) / 5; self.nodRatioHistory = []; // 重置历史,开始监测动作 self.currentPrompt = '请缓慢点头(上下移动)'; } return; } // 动态阈值(基于基准值的比例,而非固定数值) const nodUpThreshold = self.nodBaseRatio * 0.85; // 抬头时pitchRatio降低(低于基准15%) const nodDownThreshold = self.nodBaseRatio * 1.15; // 低头时pitchRatio升高(高于基准15%) // 动作趋势分析(需完成“低头→抬头”完整周期) self.nodRatioHistory = self.nodRatioHistory || []; self.nodRatioHistory.push({ ratio: pitchRatio, time: Date.now() }); if (self.nodRatioHistory.length > 8) self.nodRatioHistory.shift(); // 保留最近8帧(约640ms) // 判断是否出现“先低头(超过down阈值)再抬头(低于up阈值)”的完整趋势 const hasDown = self.nodRatioHistory.some(r => r.ratio > nodDownThreshold); const hasUp = self.nodRatioHistory.some(r => r.ratio < nodUpThreshold); // 趋势有效性验证:最高点出现在最低点之后(先低头后抬头) const downPoint = self.nodRatioHistory.find(r => r.ratio > nodDownThreshold); const upPoint = self.nodRatioHistory.findLast(r => r.ratio < nodUpThreshold); const trendValid = downPoint && upPoint && downPoint.time < upPoint.time; // 时间有效性验证(动作持续0.3-1.5秒) const actionDuration = self.nodRatioHistory.length > 1 ? self.nodRatioHistory[self.nodRatioHistory.length - 1].time - self.nodRatioHistory[0].time : 0; const timeValid = actionDuration > 300 && actionDuration < 1500; if (hasDown && hasUp && trendValid && timeValid) { self.currentPrompt = '点头完成!'; self.promptStatus = 'success'; self.goToNextStep(); // 重置状态,避免重复触发 self.nodBaseRatio = null; self.nodRatioHistory = []; } else { self.currentPrompt = '请幅度稍大点头(额头带动动作)'; self.promptStatus = 'warning'; } } // 2. 摇头检测(基于左右脸边界点的相对位置变化) else if (currentAction.type === 'shake') { const noseBridge = facePoints[27]; // 鼻梁关键点(水平位置参考) const currentX = noseBridge.x; const currentTime = Date.now(); // 1. 记录当前位置到历史数据(只保留最近10条,约800ms内的记录,与检测频率匹配) this.shakeHistory.push({ x: currentX, time: currentTime }); if (this.shakeHistory.length > 10) { this.shakeHistory.shift(); // 移除最旧的记录 } // 2. 历史数据不足时,不判定 if (this.shakeHistory.length < 5) { this.currentPrompt = '请左右摇头...'; this.promptStatus = 'warning'; return; } // 3. 分析历史数据,判断是否有“左→右”或“右→左”的摆动 const firstX = this.shakeHistory[0].x; // 最早记录的x const lastX = this.shakeHistory[this.shakeHistory.length - 1].x; // 最新记录的x const timeElapsed = currentTime - this.shakeHistory[0].time; // 动作持续时间 // 计算左右摆动的总距离(绝对值) const totalDistance = Math.abs(lastX - firstX); // 4. 判断是否完成一次完整摇头动作: // - 总距离超过最小阈值(确保有明显摆动) // - 时间在合理范围内(不超过最大时间) // - 方向发生反转(先左后右或先右后左) const isLeftToRight = firstX < lastX - this.shakeMinDistance; // 先左后右 const isRightToLeft = firstX > lastX + this.shakeMinDistance; // 先右后左 const isValidTime = timeElapsed <= this.shakeMaxTime; if ((isLeftToRight || isRightToLeft) && totalDistance >= this.shakeMinDistance && isValidTime) { // 判定为完成摇头动作 this.updateActionProgress(true, 'shake'); } else { // 未完成,提示继续 this.updateActionProgress(false, 'shake'); // 若超时未完成,重置历史记录(避免累计无效数据) if (timeElapsed > this.shakeMaxTime) { this.shakeHistory = []; this.currentPrompt = '摇头动作太慢,请重试...'; } } } // 眨眼动作增强逻辑 else if (currentAction.type === 'eye') { } // 4. 张嘴检测(新增) else if (currentAction.type === 'mouth') { // 上嘴唇关键点(索引51)和下嘴唇关键点(索引57) const upperLip = facePoints[51]; const lowerLip = facePoints[57]; // 计算嘴唇垂直距离 const mouthOpenDistance = Math.abs(upperLip.y - lowerLip.y); // 计算嘴部区域高度作为参考(上唇到下巴距离的1/3) const mouthReference = Math.abs(facePoints[27].y - facePoints[8].y) / 3; // 张嘴判定:距离超过参考值的60% const isMouthOpen = mouthOpenDistance > mouthReference * 0.6; if (isMouthOpen) { this.mouthConsecutiveFrames++; // 要求连续3帧检测到张嘴 if (this.mouthConsecutiveFrames >= this.mouthRequiredFrames) { this.updateActionProgress(true, 'mouth'); } } else { this.mouthConsecutiveFrames = Math.max(0, this.mouthConsecutiveFrames - 1); this.updateActionProgress(false, 'mouth'); } console.log(`张嘴检测:距离=${mouthOpenDistance.toFixed(2)}, 参考值=${(mouthReference * 0.6).toFixed(2)}, 是否张嘴=${isMouthOpen}`); } }, 眨眼如何判断
07-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值