Codeforces 919 D. Substring (Memory Search) __ Dynamic programming

Source: Internet
Author: User
Tags int size
Description

You are are given a graph with n nodes and M directed edges. One lowercase letter are assigned to each node. We define a path ' s value as the most frequently occurring letter. For example, if letters on a path are "Abaca", then the value of this is 3. Your task is find a path whose value is the largest.

Input

The contains two positive integers n, m (1≤n, m≤300), denoting that graph has n nodes and M directe D edges.

The second line contains a string s with only lowercase 中文版 letters. The i-th character is the letter assigned to the I-TH node.

Then m lines follow. Each line contains two integers x, y (1≤x, y≤n), describing a directed edge from X to Y. Note so x can be equal to Y And there can be multiple edges between x and Y. Also the graph can be not connected.

Output

Output a single and a single integer denoting the largest value. If the value can be arbitrarily large, output-1 instead.

examples Input

5 4
abaca
1 2
1 3
3 4 4 5

examples Output

3

the

Find a path in the diagram that satisfies the highest frequency of a letter on the path and the number of occurrences of that letter. (There may be a self loop and a heavy edge in the diagram)

train of Thought

The memory search, Dp[o][ch] D p [o] [c h] dp[o][ch] represents the maximum number of times that node o O and the following characters ch C h ch appeared.

Tarjan Ring, pay attention to the existence of the self-loop situation.

AC Code

#include <bits/stdc++.h> #define IO Ios::sync_with_stdio (false); \ cin.tie (0); \ cout.tie (0);
using namespace Std;
const int MAXN = 3E5+10;
const int mod = 1E9+7;
typedef __int64 LL;
    #define INF 0x7f7f7f struct node {int to;
int next;
} EDGE[MAXN];
int HEAD[MAXN],TOT,DFN[MAXN],LOW[MAXN];
int dp[maxn][26],idx;
int CH[MAXN],IN[MAXN];
BOOL INSTACK[MAXN];
int stack[maxn],top;
int n,m;
BOOL Flag;
Char STR[MAXN];
    void Init () {memset (dp,0,sizeof DP);
    memset (dfn,0,sizeof DFN);
    memset (low,0,sizeof low);
    memset (instack,false,sizeof instack);
    memset (head,-1,sizeof head);
    top = idx = tot = 0;
Flag = true;
    } void Addedge (int u,int v) {edge[tot].to = v;
    Edge[tot].next = Head[u];
Head[u] = tot++;
    } void Dfs (int x) {dfn[x] = low[x] = ++idx;
    Instack[x] = true;
    stack[top++] = x;
        for (int i=head[x]; I!=-1 i=edge[i].next) {int to = edge[i].to;
            if (!dfn[to]) {DFS (to); low[X] = min (low[x],low[to]);
        else if (Instack[to] && dfn[to]<low[x]) low[x] = Dfn[to];
    for (int j=0; j<26; j + +) Dp[x][j] = max (dp[x][j],dp[to][j));
        } if (Dfn[x]==low[x]) {int Size = 0,now;
            do {now = Stack[--top];
            Instack[now] = false;
        ++size;
        while (NOW!=X);
    if (size>1) flag = false;
    } if (!flag) return;
++DP[X][CH[X]];
    } void Solve () {for (int i=1; i<=n; i++) ch[i] = str[i-1]-' a ';
    int ans =-1;
            for (int i=1; i<=n; i++) if (in[i]==0) {DFS (i);
        for (int j=0; j<26; j + +) ans = max (ans,dp[i][j]);
} cout<< (Flag?ans:-1) <<endl;
    int main () {IO;
    Init ();
    cin>>n>>m;
    cin>>str;
        for (int i=0; i<m; i++) {int u,v;
       cin>>u>>v; if (U==V) flag = false;
        ++IN[V];
    Addedge (U,V);
    } solve ();
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.