Click here to Skip to main content
15,391,989 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include<queue>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
const int maxn=300005;
int cnt,head[maxn],n,m,now;
int in[maxn],dp[maxn][26];
char s[maxn];
vector<int> ans;
struct node{
int to,next;
}e[maxn*2];
void add(int u,int v){
e[cnt].to=v;
e[cnt].next=head[u],head[u]=cnt++;
}
void init(){
memset(head,-1,sizeof(head));
cnt=0;
}
int topo(){
queue<int> q;
int now=0;
for(int i=1;i<=n;i++){
if(!in[i]){
q.push(i);
dp[i][s[i]-'a']++;
}
}
while(!q.empty()){
int u=q.front(); q.pop();
now++;
for(int i=head[u];~i;i=e[i].next){
int t=e[i].to,add=s[t]-'a';
for(int j=0;j<26;j++){
dp[t][j]=max(dp[t][j],dp[u][j]+(j==add?1:0));
}
if(--in[t]==0){
q.push(t);
}
}
}
int ans=0;
for(int i=1;i<=n;i++){
for(int j=0;j<26;j++)
ans=max(ans,dp[i][j]);
}
if(now!=n) return -1;
return ans;
}
int main(){
init();
int x,y;
scanf("%d%d%s",&n,&m,s+1);
for(int i=1;i<=m;i++){
scanf("%d%d",&x,&y);
add(x,y);
in[y]++;
}
printf("%d\n",topo());
return 0;
}

What I have tried:

Convert this code into python code
Posted
Updated 4-May-21 2:58am
Comments
Manikanta Prasadam 11-May-21 8:07am
   
#include<queue>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
const int maxn=300005;
int cnt,head[maxn],n,m,now;
int in[maxn],dp[maxn][26];
char s[maxn];
vector<int> ans;
struct node{
int to,next;
}e[maxn*2];
void add(int u,int v){
e[cnt].to=v;
e[cnt].next=head[u],head[u]=cnt++;
}
void init(){
memset(head,-1,sizeof(head));
cnt=0;
}
int topo(){
queue<int> q;
int now=0;
for(int i=1;i<=n;i++){
if(!in[i]){
q.push(i);
dp[i][s[i]-'a']++;
}
}
while(!q.empty()){
int u=q.front(); q.pop();
now++;
for(int i=head[u];~i;i=e[i].next){
int t=e[i].to,add=s[t]-'a';
for(int j=0;j<26;j++){
dp[t][j]=max(dp[t][j],dp[u][j]+(j==add?1:0));
}
if(--in[t]==0){
q.push(t);
}
}
}
int ans=0;
for(int i=1;i<=n;i++){
for(int j=0;j<26;j++)
ans=max(ans,dp[i][j]);
}
if(now!=n) return -1;
return ans;
}
int main(){
init();
int x,y;
scanf("%d%d%s",&n,&m,s+1);
for(int i=1;i<=m;i++){
scanf("%d%d",&x,&y);
add(x,y);
in[y]++;
}
printf("%d\n",topo());
return 0;
}

Sorry, no. This site does not provide code conversion services.
   
This is not a code conversion service: we are not here to translate code for you.
Even if we did, what you would end up with would not be "good code" in the target language – they are based on very different frameworks, and what makes something work in one language does not always "translate" directly into another.

So what you end up with is very poor code, that is difficult if not impossible to maintain, that can’t be upgraded nicely, and that will cause you immense headaches if the original is changed. And it’ll be a nightmare to debug if it doesn’t work "straight out of the box".
Instead, use the source code as a specification for a new app written in and for the target language / framework and write it from scratch using the original as a "template". You will get a much, much better result that will save you a lot of time in the long run.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900