nxt

(模板)扩展kmp算法(luoguP5410)

大城市里の小女人 提交于 2019-12-03 09:31:59
题目链接:https://www.luogu.org/problem/P5410 题意:有两个字符串 a , b,要求输出 b与 a的每一个后缀的最长公共前缀。输出: 第一行有lenb个数,为b的next数组(特别地, next1为lenb ) 第二行有lena个数,即答案。 思路:扩展kmp模板,涉及字典树,后续再补,先放模板. AC code: #include<bits/stdc++.h> #define N 1000010 using namespace std; int q,nxt[N],extend[N]; string s,t; void getnxt() { nxt[0]=t.size();//nxt[0]一定是T的长度 int now=0; while(t[now]==t[1+now]&&now+1<(int)t.size())now++;//这就是从1开始暴力 nxt[1]=now; int p0=1; for(int i=2;i<(int)t.size();i++) { if(i+nxt[i-p0]<nxt[p0]+p0)nxt[i]=nxt[i-p0];//第一种情况 else {//第二种情况 int now=nxt[p0]+p0-i; now=max(now,0);//这里是为了防止i>p的情况 while(t[now]==t[i+now]&&i+now

【2019.11.1】ZA

拈花ヽ惹草 提交于 2019-12-03 05:22:09
noip2012 Vigenère 密码 模拟 用的以前的方法 然后我康了康yyb的 发现我好菜 int r[30][30]; void pre(){ for(int i=1;i<=26;++i) for(int j=0;j<=26-i;++j) r[i][i+j]=j+1; for(int i=2;i<=26;++i) for(int j=28-i;j<=26;++j) r[i][j-27+i]=j; } int main(){ #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif pre(); scanf("%s%s",k,c); lk=strlen(k),lc=strlen(c); for(int i=0,cur=0,x,y,xx;i<lc;++i,++cur){ if(cur>=lk) cur=0;xx=0; if(c[i]>='A'&&c[i]<='Z') x=c[i]-'A'+1; else if(c[i]>='a'&&c[i]<='z') xx=1,x=c[i]-'a'+1; if(k[cur]>='A'&&k[cur]<='Z') y=k[cur]-'A'+1; else if(k[cur]>='a'&&k[cur]<='z') y=k[cur]-'a'+1; if(xx) printf("%c",r[y]

HDU 2899 Strange fuction (模拟退火)

巧了我就是萌 提交于 2019-12-03 02:44:38
题目链接: HDU 2899 Problem Description Now, here is a fuction: F(x) = 6 * x^7+8 x^6+7 x^3+5 x^2-y x (0 <= x <=100) Can you find the minimum value when x is between 0 and 100. Input The first line of the input contains an integer T(1<=T<=100) which means the number of test cases. Then T lines follow, each line has only one real numbers Y.(0 < Y <1e10) Output Just the minimum value (accurate up to 4 decimal places),when x is between 0 and 100. Sample Input 2 100 200 Sample Output -74.4291 -178.8534 Solution 题意 给定 \(y\) ,求函数 \(F(x) = 6x^7 + 8x^6 + 7x^3 + 5x^2 - yx\) 的最小值,其中 \(x\) 的范围是 \([0, 100]\) 。

How to fix gcc error: expected while before void

匿名 (未验证) 提交于 2019-12-03 02:16:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: So I am writing a peer-to-peer chat client that uses pthreads to manage all the IO and when I compile the file gcc gives me the error client.c: In function ‘accepted_daemon’: client.c:115:1: error: expected ‘while’ before ‘void’ void * ^ client.c: In function ‘listen_daemon’: client.c:176:1: error: expected ‘while’ before ‘int’ int main(int argc, char *argv[]) ^ The source code for my program is #include <stdlib.h> #include <string.h> #include <stdint.h> #include <errno.h> #include <assert.h> #include <unistd.h> #include <pthread.h> #include

[LOJ#6468.] 魔法

若如初见. 提交于 2019-12-03 01:40:17
官方题解 看了题解才会做.. 首先考虑如果所有询问的点都是[1,n]的做法,如果询问是[l,r]只需要把多余的去掉就好了 然后要把问题转化为一个点对其他附近的点的贡献 记$pre[i]$为第i个位置的数字上一次出现的位置,记$nxt[i]$为第i个位置上的数字下一次出现的位置, 显然这些东西都能扫一遍求出来 然后对于一个点,它能做出贡献的区间是:$$[\frac{pre[i]+i}{2}+1,\frac{i+nxt[i]}{2}]$$ 来源: https://www.cnblogs.com/zhenglw/p/11769611.html

Tensorflow feature columns in Dataset map Table already initialized issue

匿名 (未验证) 提交于 2019-12-03 00:57:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I've run into an issue trying to use Tensorflow's feature_column mappings inside of a function passed in to the Dataset map method. This happens when trying to one hot encode categorical string features of a Dataset as part of the input pipeline using Dataset.map. The error message I'm getting is that: tensorflow.python.framework.errors_impl.FailedPreconditionError: Table already initialized. The following code is a basic example that recreates the problem: import numpy as np import tensorflow as tf from tensorflow.contrib.lookup import

loj#2721. 「NOI2018」屠龙勇士

匿名 (未验证) 提交于 2019-12-03 00:42:01
loj#2721. 「NOI2018」屠龙勇士 首先可以列出线性方程组 方程组转化为在模p意义下的同余方程 因为不保证pp 互素,考虑扩展中国剩余定理合并 方程组是带系数的,我们要做的是在%p意义下把系数除过去,(系数为atk[i]) (atk[i],p[i]) 不等于1时无逆元,此时仍可能有解 很显然无解的情况就是 瞎jb猜的,无解的话就是%p[i]意义下atk[i] != 0 ,a[i] = 0 考虑原方程式ai = atk{i] * x + p[i] * y 方程两边同除gcd(pi,atki)解不变 此时atki,pi此时保证了atki与pi互素 若ai不能被整除也是无解的 扩展crt 着实被winXP下输出lld坑了一把....顺带被自己抄的splay坑了一把 /* 苟活者在淡红的血色中,会依稀看到微茫的希望 */ #include<bits/stdc++.h> using namespace std; inline long long read() { long long x; scanf("%lld",&x); return x; } int n,m; #define LL long long const int maxn = 500007; LL a[maxn],p[maxn]; // x atk[i] = a[i] ( % p[i]) LL atk[maxn]

树套树-线段树套平衡树

匿名 (未验证) 提交于 2019-12-02 23:55:01
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<vector> #include<cmath> #include<map> #include<bitset> #pragma GCC optimize(2) #define rep(i,a,b) for(int i=(a);i<=(b);++i) #define dwn(i,a,b) for(int i=(a);i>=(b);--i) using namespace std; typedef long long ll; const int N=6000000,inf=2147483647; int n,m,maxn,a[N+10]; int tot,rt[N+10],sz[N+10],rec[N+10],v[N+10],fa[N+10],ch[N+10][2]; inline int read() { int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { x=(x<<3)+(x<<1)+(ch^48); ch=getchar(); }

字符串模板--KMP

匿名 (未验证) 提交于 2019-12-02 23:51:01
1 void getnext() 2 { 3 int k=-1,i=0; 4 nxt[0]=-1; 5 while(i<lent){ 6 while(k>=0&&t[k]!=t[i]) 7 k=nxt[k]; 8 i++,k++; 9 nxt[i]=k; 10 } 11 } 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 using namespace std; 5 #define e exit(0) 6 #define R register 7 const int maxn=1e6+10; 8 char s[maxn],t[maxn]; 9 int lens,lent,deep,nxt[maxn],pos[maxn]; 10 void getnext() 11 { 12 int k=-1,i=0; 13 nxt[0]=-1; 14 while(i<lent){ 15 while(k>=0&&t[k]!=t[i]) 16 k=nxt[k]; 17 i++,k++; 18 nxt[i]=k; 19 } 20 } 21 void kmp() 22 { 23 int i=0,j=0; 24 while(j<lens){ 25 while(i>=0&&t[i]!=s[j]) 26 i=nxt[i]; 27 i++

KMP入门

匿名 (未验证) 提交于 2019-12-02 23:49:02
#include <iostream> #include <cstring> using namespace std ; char s [ 1000 ], p [ 1000 ]; inline int getans ( char * s , char * p ){ int sl = strlen ( s ), pl = strlen ( p ); int i = 0 , j = 0 ; while ( i < sl && j < pl ){ if ( s [ i ]== p [ j ]) i ++, j ++; else { i = i - j + 1 ; j = 0 ; } } if ( j == pl ) return i - j ; else return - 1 ; } int main (){ cin >> s >> p ; int ans = getans ( s , p ); cout << ans << endl ; return 0 ; } inline int KMPsearch ( char * s , char * p ){ int sl = strlen ( s ), pl = strlen ( p ); int i = 0 , j = 0 ; while ( i < sl && j < pl ){ if ( s [ i ]== p [ j ]|| j =