acdream 1019: Palindrome 字符串多项式取Hash值

1019: Palindrome

Time Limit: 1 Sec  Memory Limit: 128 MB 

Description

Now we have a long long string, and we will have two kinds of operation on it.
C i y: change the ith letter to y;
Q i j: check whether the substring from ith letter to jth letter is a palindrome.
 
 

 

Input

There are multiple test cases.
The first line contains a string whose length is not large than 1,000,000.
The next line contains a integer N indicating the number of operations.
The next N lines each lines contains a operation.
All letters in the input are lower-case.

 

Output

For each query operation, output "yes" if the corresponding substring is a palindrome, otherwise output "no".

 

Sample Input

aaaaa 4 Q 1 5 C 2 b Q 1 5 Q 1 3

Sample Output

yes no yes

HINT

 

Source

Adapted from a problem by yy17yy, 3x......
 
题目大意: 输入一个10^6长字符串,然后M种操作,Q(i,j)为查询(i,j)的字串是否为回文串,C(i,ch)为将i位置字符改为ch。
 
解题思路:若字符串为回文串,则其正序的hash值与其逆序的hash值相同。在这里我们使用多项式差值取模来求子串hash值,因为字符可限定为[1,26],所以基数设定为27。
 
假设字符串为(a1,a2,a3,a4,..,an)  其hash编码为 a1*pow(27,0)+a2*pow(27,1)+..+an*pow(27,n-1) , 例如2进制表示一个序列,若不相等必定不会出现重复hash值。一样的原理。理论上如此,但是这里pow(27,10^6) 数值太大,必定溢出,为何不会出错我估计是数据不够大吧~~~
 
通过hash值来简化字符串回文判定问题。使用树状数组来处理修改和数组和相加问题。时间复杂度O(m*log(n))
 
View Code
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #include<math.h>
 5 #include<iostream>
 6 using namespace std;
 7 
 8 typedef unsigned long long LL;
 9 const int N = 1000007;
10 
11 LL bit[N], C[2][N];
12 int n, m;
13 char str[N];
14 void update( int x, LL val, int flag )
15 {
16     for( ; x <= n; x += x&(-x) )
17         C[flag][x] += val;
18 }
19 LL sum( int x, int flag )
20 {
21     LL res = 0;
22     for( ; x >= 1; x -= x&(-x) )
23         res += C[flag][x];
24     return res;
25 }
26 void InitBit(){
27     bit[0] = 1;
28     for(int i = 1; i < N; i++)
29         bit[i] = bit[i-1]*27;
30 }
31 void init(){
32     n = strlen( str );
33     memset( C, 0, sizeof(C) );
34     for(int i = 1; i <= n; i++)
35     {
36         int j = (n+1)-i;    
37         update( i, (str[i-1]-'a'+1)*bit[i-1], 0 );
38         update( j, (str[i-1]-'a'+1)*bit[j-1], 1 ); 
39     }
40 }
41  
42 void solve(){
43     scanf("%d", &m);
44     char op[2], ch[2]; 
45     int a, b, c, d;
46     while( m-- )
47     {
48         scanf("%s", op);
49         if( op[0] == 'Q' )
50         {
51             scanf("%d%d",&a,&b);
52             if(a > b) swap(a,b);    
53             d = (n+1)-a; c = (n+1)-b;
54             LL hash0 = ( sum(b,0)-sum(a-1,0) )*bit[c-1];//对阶    
55             LL hash1 = ( sum(d,1)-sum(c-1,1) )*bit[a-1];    
56             puts( hash0 == hash1 ? "yes" : "no" );    
57         }
58         else
59         {
60             scanf("%d%s",&a,ch);
61             b = (n+1)-a;
62             update( a, (ch[0]-str[a-1])*bit[a-1], 0 );
63             update( b, (ch[0]-str[a-1])*bit[b-1], 1 );
64             str[a-1] = ch[0];    
65         }
66     }
67 }
68 int main(){
69     InitBit();    
70     while( scanf("%s",str) != EOF)
71     {
72         init();
73         solve();
74     }
75     return 0;
76 }

 

 
 

转载于:https://www.cnblogs.com/yefeng1627/archive/2012/12/08/2809078.html

使用优化算法,以优化VMD算法的惩罚因子惩罚因子 (α) 和分解层 (K)。 1、将量子粒子群优化(QPSO)算法与变分模态分解(VMD)算法结合 VMD算法背景: VMD算法是一种自适应信号分解算法,主要用于分解信号为不同频率带宽的模态。 VMD的关键参包括: 惩罚因子 α:控制带宽的限制。 分解层 K:决定分解出的模态。 QPSO算法背景: 量子粒子群优化(QPSO)是一种基于粒子群优化(PSO)的一种改进算法,通过量子行为模型增强全局搜索能力。 QPSO通过粒子的量子行为使其在搜索空间中不受位置限制,从而提高算法的收敛速度与全局优化能力。 任务: 使用QPSO优化VMD中的惩罚因子 α 和分解层 K,以获得信号分解的最佳效果。 计划: 定义适应度函:适应度函根据VMD分解的效果来定义,通常使用重构信号的误差(例如均方误差、交叉熵等)来衡量分解的质量。 初始化QPSO粒子:定义粒子的位置和速度,表示 α 和 K 两个参。初始化时需要在一个合理的范围内为每个粒子分配初始位置。 执行VMD分解:对每一组 α 和 K 参,运行VMD算法分解信号。 更新QPSO粒子:使用QPSO算法更新粒子的状态,根据适应度函调整粒子的搜索方向和位置。 迭代求解:重复QPSO的粒子更新步骤,直到满足终止条件(如适应度函达到设定阈,或最大迭代次)。 输出优化结果:最终,QPSO算法会返回一个优化的 α 和 K,从而使VMD分解效果最佳。 2、将极光粒子(PLO)算法与变分模态分解(VMD)算法结合 PLO的优点与适用性 强大的全局搜索能力:PLO通过模拟极光粒子的运动,能够更高效地探索复杂的多峰优化问题,避免陷入局部最优。 鲁棒性强:PLO在面对高维、多模态问题时有较好的适应性,因此适合海上风电时间序列这种非线性、多噪声的据。 应用场景:PLO适合用于优化VMD参(α 和 K),并将其用于风电时间序列的预测任务。 进一步优化的建议 a. 实现更细致的PLO更新策略,优化极光粒子的运动模型。 b. 将PLO优化后的VMD应用于真实的海上风电据,结合LSTM或XGBoost等模型进行风电功率预测。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值