FZU-1926+KMP

题意:给定一篇文章和一些句子。询问句子是否在文章中出现。

kmp模板题

 

/*
kmp
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<math.h>
using namespace std;
typedef long long int64;
//typedef __int64 int64;
typedef pair<int64,int64> PII;
#define MP(a,b) make_pair((a),(b)) 
const int maxn = 1005;
const int maxm = 105;
const int inf = 0x7fffffff;
const double pi=acos(-1.0);
const double eps = 1e-8;

char text[ maxn ][ maxm ];
int len_text;
char pat[ maxn ][ maxm ];
int len_pat;
int next[ maxn ];

void getnext(){
	int i,j;
	next[ 0 ] = -1;
	i = 0;
	j = -1;
	while( i<len_pat ){
		if( j==-1||strcmp( pat[i],pat[j] )==0||strcmp( pat[j],"_" )==0||strcmp( pat[i],"_" )==0 ){
			i ++ ;
			j ++ ;
			next[ i ] = j;
		}
		else
			j = next[ j ];
	}
	return ;
}

bool kmp( ){
	getnext();
	//for( int i=0;i<len_pat;i++ ){
	//	printf("next[ %d ] = %d \n",i,next[i]);
	//}
	int i,j;
	i = 0;
	j = 0;
	while( i<len_text&&j<len_pat ){
		if( j==-1||strcmp( text[i],pat[j] )==0||strcmp(pat[j],"_")==0 ){
			i ++ ;
			j ++ ;
		}
		else 
			j = next[j];
		if( j==len_pat ) return true;
	}
	return false;
}
	
int main(){
	int T;
	scanf("%d",&T);
	int Case = 1;
	while( T-- ){
		len_text = 0;
		while( 1 ){
			scanf("%s",text[ len_text ]);
			if( strcmp( text[ len_text ],"@" )==0 ){
				break;
			}
			len_text ++ ;
		}//input the passage
		int m;
		scanf("%d",&m);
		printf("Case %d:\n",Case++);
		while( m-- ){
			len_pat = 0;
			while( 1 ){
				scanf("%s",pat[ len_pat ]);
				if( strcmp( pat[ len_pat ],"@" )==0 ){
					break;
				}
				len_pat ++ ;
			}
			bool flag = kmp();
			if( flag ) puts("YES");
			else puts("NO");
		}
	}
	return 0;
}


 

 

内容概要:本文提出了一种基于融合鱼鹰算法和柯西变异的改进麻雀优化算法(OCSSA),用于优化变分模态分解(VMD)的参数,进而结合卷积神经网络(CNN)与双向长短期记忆网络(BiLSTM)构建OCSSA-VMD-CNN-BILSTM模型,实现对轴承故障的高【轴承故障诊断】基于融合鱼鹰和柯西变异的麻雀优化算法OCSSA-VMD-CNN-BILSTM轴承诊断研究【西储大学数据】(Matlab代码实现)精度诊断。研究采用西储大学公开的轴承故障数据集进行实验验证,通过优化VMD的模态数和惩罚因子,有效提升了信号分解的准确性与稳定性,随后利用CNN提取故障特征,BiLSTM捕捉时间序列的深层依赖关系,最终实现故障类型的智能识别。该方法在提升故障诊断精度与鲁棒性方面表现出优越性能。; 适合人群:具备一定信号处理、机器学习基础,从事机械故障诊断、智能运维、工业大数据分析等相关领域的研究生、科研人员及工程技术人员。; 使用场景及目标:①解决传统VMD参数依赖人工经验选取的问题,实现参数自适应优化;②提升复杂工况下滚动轴承早期故障的识别准确率;③为智能制造与预测性维护提供可靠的技术支持。; 阅读建议:建议读者结合Matlab代码实现过程,深入理解OCSSA优化机制、VMD信号分解流程以及CNN-BiLSTM网络架构的设计逻辑,重点关注参数优化与故障分类的联动关系,并可通过更换数据集进一步验证模型泛化能力。
### 加密与解密算法的实现及推导 加密和解密是密码学中的核心概念。在本例中,使用了线性同余变换 \(C = (c \cdot P + m) \mod 26\) 进行加密,其中 \(P\) 是明文字母对应的数值(A=0, B=1, ..., Z=25),\(C\) 是密文字母对应的数值,\(c\) 和 \(m\) 是加密参数,26 表示英文字母表的长度。 #### 加密过程 给定加密公式 \(C = (7 \cdot P + 5) \mod 26\),需要将明文 "FZU" 转换为对应的数值,并代入公式计算。 - 明文字母 "F" 对应数值 \(P = 5\)。 - 将 \(P = 5\) 代入公式: \[ C = (7 \cdot 5 + 5) \mod 26 = 40 \mod 26 = 14 \] 数值 14 对应字母 "O"。 - 明文字母 "Z" 对应数值 \(P = 25\)。 - 将 \(P = 25\) 代入公式: \[ C = (7 \cdot 25 + 5) \mod 26 = 180 \mod 26 = 0 \] 数值 0 对应字母 "A"。 - 明文字母 "U" 对应数值 \(P = 20\)。 - 将 \(P = 20\) 代入公式: \[ C = (7 \cdot 20 + 5) \mod 26 = 145 \mod 26 = 17 \] 数值 17 对应字母 "R"。 因此,明文 "FZU" 的加密结果为 "OAR"。 #### 解密公式的推导 解密公式的形式为 \(P = a \cdot (C - b) \mod 26\),其中 \(a\) 是 \(c\) 的模逆元(即满足 \(a \cdot c \equiv 1 \mod 26\) 的整数),\(b\) 是加密公式中的偏移量。 - 首先求 \(c = 7\) 在模 26 下的逆元。通过扩展欧几里得算法或穷举法可得 \(a = 15\),因为 \(7 \cdot 15 \equiv 1 \mod 26\)[^4]。 - 然后确定解密公式为: \[ P = 15 \cdot (C - 5) \mod 26 \] #### 解密过程 使用上述解密公式对密文 "OAR" 进行解密: - 密文字母 "O" 对应数值 \(C = 14\)。 - 将 \(C = 14\) 代入公式: \[ P = 15 \cdot (14 - 5) \mod 26 = 15 \cdot 9 \mod 26 = 135 \mod 26 = 5 \] 数值 5 对应字母 "F"。 - 密文字母 "A" 对应数值 \(C = 0\)。 - 将 \(C = 0\) 代入公式: \[ P = 15 \cdot (0 - 5) \mod 26 = 15 \cdot (-5) \mod 26 = -75 \mod 26 = 25 \] 数值 25 对应字母 "Z"。 - 密文字母 "R" 对应数值 \(C = 17\)。 - 将 \(C = 17\) 代入公式: \[ P = 15 \cdot (17 - 5) \mod 26 = 15 \cdot 12 \mod 26 = 180 \mod 26 = 20 \] 数值 20 对应字母 "U"。 因此,密文 "OAR" 的解密结果为 "FZU"。 ### 示例代码 以下是用 Python 实现加密和解密的代码: ```python def encrypt(plaintext, c, m): ciphertext = "" for char in plaintext: if 'A' <= char <= 'Z': P = ord(char) - ord('A') C = (c * P + m) % 26 ciphertext += chr(C + ord('A')) else: ciphertext += char return ciphertext def decrypt(ciphertext, c_inv, m): plaintext = "" for char in ciphertext: if 'A' <= char <= 'Z': C = ord(char) - ord('A') P = (c_inv * (C - m)) % 26 plaintext += chr(P + ord('A')) else: plaintext += char return plaintext # 参数设置 c = 7 m = 5 c_inv = 15 # 模逆元 # 测试加密 plaintext = "FZU" ciphertext = encrypt(plaintext, c, m) print("加密结果:", ciphertext) # 测试解密 decrypted_text = decrypt(ciphertext, c_inv, m) print("解密结果:", decrypted_text) ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值