Codeforces Gym 100345I Segment transformations [idea title]

Source: Internet
Author: User

Test Instructions Brief Introduction

Given a C-G t Four letter cipher lock (per toggle, A to C C to G G to t t change a)

The password lock has n-bits, and each operation can be selected with a continuous toggle

Ask at least several operations to change the initial state to the end state

And the output of each operation

(This topic has SPJ)

---------------------------------------------------------------------------------------------------------

For the convenience of describing all locations that are not clearly stated, they are in the mod4 sense.

First of all, we can clearly understand that the order of operation of a nature is not related.

It's only the number of times each position has been touched.

At the beginning of the game, it was a random greed, but there was a counter-example.

At the end of the 30min, I thought of the range DP from the data.

However, the problem is not a normal interval DP

The minimum number of operations is good but the operation scheme is difficult to record

When it comes to the problem, I finally think of a better idea of greed.

---------------------------------------------------------------------------------------------------------

We use a height array h to record the number of times each position needs to be toggled from the initial state to the final state.

Like the example

Aggtcat

Aaactaa

The height array h is 0222201

We'll define a Delta array to represent all H[i]-h[i-1]

So the value of the delta array from 1 to N+1 is 02000213.

(such constructs are similar to the practice of maintaining intervals and reducing values with a tree array but I don't know the exact name of the idea.)

Obviously we can change two 2 to 0 or 1 and 3 to 0 each time.

So you can do it?

But it would be RE11 to do just that.

---------------------------------------------------------------------------------------------------------

such as this example

Aa

GT

Height array h is 23

The delta array is 211

So it's not possible to find two or 2 or 11 three-match elimination

Since we can't eliminate two at a time, let's do it one at a time.

But obviously it doesn't make some of the eliminated parts appear again.

So just look for two non-0 deals and turn one of them into 0.

(Note that the sum of the delta array is 0, so there will be no more than a 0 of the last one)

#include <bits/stdc++.h>using namespacestd;Const intn= the;CharS1[n],s2[n];inth[n],delta[n],cnt[4];intL[n],r[n],d[n];intN,ans;intMain () {#ifdef Online_judge freopen ("transform.in","R", stdin); Freopen ("Transform.out","W", stdout);#endifscanf ("%s%s", &s1[1],&s2[1]); N=strlen (&s1[1]);  for(intI=1; i<=n;++i) {if(s1[i]=='A') H[i]=-0; Else if(s1[i]=='C') H[i]=-1; Else if(s1[i]=='G') H[i]=-2; ElseH[i]=-3; if(s2[i]=='A') H[i]+=0; Else if(s2[i]=='C') H[i]+=1; Else if(s2[i]=='G') H[i]+=2; ElseH[i]+=3; H[i]=h[i]<0? h[i]+4: H[i]; }         for(intI=1; i<=n+1;++i) {Delta[i]= (h[i]-h[i-1]+4)%4; Cnt[delta[i]]++; }         while(cnt[0]!=n+1)        {            ++ans; intI=1;  while(!Delta[i])++i; intj=i+1;  while(delta[j]+delta[i]!=4&&j<=n+1)                ++J; if(j<=n+1) {L[ans]=i; R[ans]=j-1; D[ans]=Delta[i]; Cnt[delta[i]]--; CNT[DELTA[J]]--; cnt[0]+=2; Delta[i]=delta[j]=0; }            Else{J=i+1;  while(!Delta[j])++J; L[ans]=i; R[ans]=j-1; D[ans]=Delta[i]; Cnt[delta[i]]--; CNT[DELTA[J]]--; cnt[0]++; cnt[(Delta[j]+delta[i])%4]++; DELTA[J]= (Delta[j]+delta[i])%4; Delta[i]=0; }} printf ("%d\n", ans);  for(intI=1; i<=ans;++i) printf ("%d%d%d\n", L[i],r[i],d[i]); return 0;}

Codeforces Gym 100345I Segment transformations [idea title]

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.