Test instructions: A forward graph, each node is given a lowercase letter, the value of a path and the number of letters that appear on this path, the maximum value of the graph
Match, with Dfs timeout, see the official work of the DP and topological sorting, a--z with 0-25, using dp[i][j] to indicate the number of the path at the end of the first node of the first J letter Occurrences
Topology sort each row to a point, with the DP of the point to update the DP with its adjacent point, the first entry to 0 of the point special processing, DP process synchronization Update Results res
Also reviewed the topology sort
#include <iostream>#include<cstdio>#include<cstring>#include<vector>#include<queue>using namespacestd;#defineNnum 300005CharCh[nnum];intdp[nnum][ -];vector<int>Eage[nnum];intDeg[nnum];intN,m,res;BOOLDeg0[nnum];BOOLToposort () {intCnt=0; Queue<int>que; for(intI=1; i<=n; i++) if(deg[i]==0) {CNT++; Que.push (i); Deg0[i]=1; } ElseDeg0[i]=0; while(!Que.empty ()) { intnow=Que.front (); Que.pop (); if(deg0[now]==1)//The initial entry is 0 points, its DP needs to attach the initial value, and then update the adjacent node Dp[now][ch[now-1]-'a']++; for(intI=0; I<eage[now].size (); i++) { for(intj=0; j< -; J + +) { inttmp=ch[eage[now][i]-1]-'a'; if(j==tmp) DP[EAGE[NOW][I]][TMP]=max (dp[eage[now][i]][tmp],dp[now][tmp]+1); ElseDp[eage[now][i]][j]=Max (dp[eage[now][i]][j],dp[now][j]); Res=Max (res,dp[eage[now][i]][j]); } //cout<< "*" <<res<<endl;deg[eage[now][i]]--; if(deg[eage[now][i]]==0) {Que.push (eage[now][i]); CNT++; } } } if(cnt==N)return 1; Else return 0;}intMain () { while(SCANF ("%d%d", &n,&m)! =EOF) { for(intI=0; i<=n;i++) eage[i].clear (); Res=0; memset (deg,0,sizeof(deg)); Memset (DP,0,sizeof(DP)); scanf ("%s", CH); for(intI=0; i<m; i++) { intb; scanf ("%d%d",&a,&b); DEG[B]++; Eage[a].push_back (b); } if(Toposort ()) printf ("%d\n", RES); Elseprintf ("-1\n"); } return 0;}
Codeforces Round #460 (Div. 2) _d. substring_[dp][topology Sort]