
Beautiful Function
C++
// codewindow.in // Beautiful function // code in Cpp #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; }
Follow us
Also Checkout
- UKG Hiring for Software Engineer
- Unlocking Innovation and Diversity: Accenture HackDiva Empowers Women in Tech with Cutting-Edge Solutions – codewindow.in
- QA Engineer Opportunities at Siemens Company: Apply Now – codewindow.in
- Transform Your Career with S&P Global: Apply for the Software Development Engineer Role and Lead the Future of Financial Technology Innovation – codewindow.in
- Unlock Your Potential at Accenture as an Associate Software Engineer – Elevate Your Career with Innovation and Excellence – codewindow.in
- Accelerate Your Career: Join NVIDIA’s Elite Software Engineering Internship Program and Shape the Future of Technology – codewindow.in
- Software Engineer at ABB: Unlock Innovation and Shape the Future – codewindow.in
- IBM Associate Systems Engineer Job: Boost Your Career with a Leading Technology Giant – codewindow.in
- Start Your IT Career Journey with Amazon: IT Services Support Associate I Opportunity – codewindow.in
- Shape the Future of Web: Front-End Software Engineer Opportunity at Google Cloud – codewindow.in
- Exciting Opportunity: Java Spring Boot Senior Developer Role at Infosys – codewindow.in
- Unlock Your Potential at Nokia: Software Engineer Opportunities Await – codewindow.in
- Join Microsoft’s World-Class Team as a Software Engineer and Shape the Future of Technology – codewindow.in
- Virtusa is Seeking Talented React JS Developers to Drive Digital Excellence – codewindow.in
- Join IBM Dynamic Team as a Full Stack Developer and Shape the Future – codewindow.in
- EY Welcomes Aspiring AI/ML Interns to Unlock the Future of -codewindow.in
- Wipro is Hiring Test Engineers to Elevate Quality Assurance – codewindow.in
- Deloitte Is Hiring: Analysts and Senior Analysts Wanted to Drive Innovation – codewindow.in
- Exciting Software Development Opportunity At Oracle – codewindow.in
- PayPal Hiring Data Analyst 1 – codewindow.in
- SIEMENS HEALTHINEERS Hiring Data Scientist ( ML & AI) – codewindow.in
- Programming in C++ – codewindow.in
- Programming in C++ – codewindow.in
- Unstop Hiring Challenges Internships and Hackathons – codewindow.in
- Programming in Python – codewindow.in
- Programming in Python – codewindow.in
- Programming in C++ – codewindow.in
- Programming in C++ – codewindow.in
- Programming in C++ – codewindow.in
- Programming in C++ – codewindow.in
- Programming in C++ – codewindow.in
- Data Structure – comprehensive guide – codewindow.in
- Programming in C++ – codewindow.in
- Programming in C++ – codewindow.in
- Programming in C++ – codewindow.in
- Programming in C++ – codewindow.in
- Automata Fixing – codewindow.in
- Codewindow Mock Test 1
- Programming in C++ – codewindow.in
- Programming in C++ – codewindow.in
- Pseudocode- codewindow.in
- Programming in C++ – codewindow.in
- Aptitude tricks – codewindow.in
- Aptitude Tricks – codewindow.in
- Test series – codewindow.in
- Aptitude Tricks – codewindow.in
- C++ Technical Questions – codewindow.in
- Programming in C++ – codewindow.in
- Programming in C++ – codewindow.in
- Programming in C++ – codewindow.in