int strStr(char *haystack, char *needle) { if(needle[0] == '\0') return 0; int m = strlen(haystack); int n = strlen(needle); vector<int> nxt(n + 1); calNext(needle, n, nxt); int i = 0; int j = 0; while(i < m) { if(j == -1 || haystack[i] == needle[j]) { ++i; ++j; } else { j = nxt[j]; } if(j == n) return i - j; } return -1; }
int main(){ int i,j,k; srand( (unsigned)time( NULL ) ); scanf("%d%d",&n,&m); m1=m+1; for(i=1;i<=n;i++){scanf("%d",&a[i]);} for(i=1;i<=m;i++){scanf("%d",&b[i]);pb[i]=i;} for(i=1;i<=n;i++)for(j=10000;j>=0;j--)if(cr[j]==1)cr[j+a[i]]=1; for(i=1;i<=n;i++)for(j=1;j<=n;j++)if(a[i]>a[j]){swap(&a[i],&a[j]);} for(i=1;i<=m;i++){ for(j=1;j<=n;j++){ if(a[j]<=b[i]){ sf[i]=j; break; } } } for(k=1;k<=n;k++){ bag[k][0]=1; for(i=k;i<=n;i++){ for(j=100;j>=0;j++)if(bag[k][j]==1)bag[k][j+a[i]]=1; } } for(i=1;i<=1000000;i++)nxt(); }
static int utl_pmx_isin(const char *pat, const char *pat_end, int32_t ch, int32_t (*nxt)(const char *, int32_t *)) { int32_t c1,c2; int32_t len; while (pat<pat_end) { c2=0; c1=0; len = nxt(pat, &c1); if (ch == c1) return 1; pat +=len; if (*pat == '-') { len = nxt(++pat, &c2); if (c1 <= ch && ch <= c2) return 1; pat += len; } } return 0; }
//! <b>Effects</b>: Reverses the order of elements in the list. //! //! <b>Throws</b>: Nothing. //! //! <b>Complexity</b>: This function is linear to the contained elements. static void reverse(node_ptr p) { node_ptr i = NodeTraits::get_next(p), e(p); for (;;) { node_ptr nxt(NodeTraits::get_next(i)); if (nxt == e) break; base_t::transfer_after(e, i, nxt); } }
FTYPE max_flow(int s, int t) { vector<int> ptr(g.V, 0), h(g.V, 0), nxt(g.V, -1), hv(2*g.V, -1); vector<FTYPE> e(g.V, 0); h[s] = g.V; for (int i: g.adj[s]) { int w = g.edges[i].v; if (!g.edges[i].cap) continue; if (!e[w] && w != t) { nxt[w] = hv[0]; hv[0] = w; } e[w] += g.edges[i].cap; e[s] -= g.edges[i].cap; g.edges[i^1].cap = g.edges[i].cap; g.edges[i].cap = 0; } int cur_h = 0; while (cur_h >= 0) { for (int v = hv[cur_h]; v != -1; v = nxt[v]) { for (int &p = ptr[v]; p < (int)g.adj[v].size(); p++) { int i = g.adj[v][p]; int w = g.edges[i].v; if (h[w] < h[v] && g.edges[i].cap) { FTYPE f = min(g.edges[i].cap, e[v]); g.edges[i].cap -= f; g.edges[i^1].cap += f; if (!e[w] && w != t) { nxt[w] = hv[h[w]]; hv[h[w]] = w; } e[w] += f; e[v] -= f; if (e[v] == 0) break; } } hv[cur_h] = nxt[v]; if (e[v]) { ptr[v] = 0; h[v]++; nxt[v] = hv[h[v]]; hv[h[v]] = v; cur_h++; break; } } if (hv[cur_h] == -1) cur_h--; } return e[t]; }
static int gr_nextinout(lua_State *L, edge_first_iter_t *fst, edge_next_iter_t *nxt) { int rv; Agedge_t *e; char sbuf[32]; gr_edge_t *ud_e; gr_node_t *ud_n = tonode(L, 1, STRICT); Agraph_t *g = agroot(ud_n->n); if (lua_isnil(L, 2)) e = fst(g, ud_n->n); else { ud_e = toedge(L, 2, STRICT); e = nxt(g, ud_e->e); } if (!e){ /* no more nodes */ lua_pushnil(L); return 1; } else { /* Check whether userdata exists .. */ rv = get_object(L, e); if (rv == 1) /* .. yes: return it */ return rv; else { /* .. no: create it */ lua_pop(L, rv); ud_e = lua_newuserdata(L, sizeof(gr_edge_t)); ud_e->e = e; sprintf(sbuf, "edge@%lu", (unsigned long) AGID(e)); ud_e->name = strdup(sbuf); ud_e->type = AGEDGE; set_object(L, e); return new_edge(L); } } }
int main(){ int i,j,k,t; srand( (unsigned)time( NULL ) ); scanf("%d%d",&n,&m); m1=m+1; for(i=1;i<=n;i++){scanf("%d",&a[i]);} for(i=1;i<=m;i++){scanf("%d",&b[i]);pb[i]=i;} for(i=1;i<=n;i++)for(j=1;j<=n;j++)if(a[i]>a[j]){swap(&a[i],&a[j]);} for(i=1;i<=m;i++){ for(j=1;j<=n;j++){ if(a[j]<=b[i]){ sf[i]=j; break; } } } bag[n+1][0]=1;syg[n+1][0]=n+1; for(k=n;k>=1;k--){ for(j=10000;j>=0;j--)if(bag[k+1][j]!=0){ if(bag[k+1][j]==1){ bag[k][j+a[k]]+=bag[k+1][j]; y[k][j+a[k]]=1; from[k][j+a[k]]=syg[k+1][j]; syg[k][j+a[k]]=k; syg[k][j]=syg[k+1][j]; if(a[k]!=a[k+1]||y[k+1][j]==0){ bag[k][j]+=bag[k+1][j]; }else{ bag[k][j]=0; } }else{ bag[k][j+a[k]]=bag[k+1][j]; bag[k][j]=bag[k+1][j]; } } } for(i=0;i<=150;i++){lbl[i+1]=i;lbr[i]=i+1;} for(i=1;i<=m;i++){ if(bag[1][b[i]]==1){ nomove[i]=1; k=b[i]; j=syg[1][k]; while(k>0){ t=j; while(hold[t])t++; hold[t]=1; c[t]=pb[i]; pc[t]=a[t]; bagskip[j]++; lbr[lbl[t]]=lbr[t]; lbl[lbr[t]]=lbl[t]; k-=a[j]; j=from[j][k+a[j]]; } } } for(i=1;i<=10000000;i++){ nxt(); //printf("%d\n",i); } }