1151A - Maxim and Biology - CodeForces Solution


brute force strings *1000

Please click on ads to support us..

Python Code:



n = int(input())
chaine = input()


lettres = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"


def moves(a, b):
    
    i = lettres.index(a)
    j = lettres.index(b)
    return min(abs(i - j), 26 - abs(i - j))


mini = 1e10
for i in range(n - 3):
    som = 0
    for j, c in enumerate("ACTG"):
        som += moves(chaine[i + j], c)
    if som < mini:
        mini = som

print(mini)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define  ll long long int 
#define  all(x)  x.begin(),x.end()
#define  ll long long int  
int const mod = 1e9+7; 

ll dis (char c1, char c2 ) {
	  ll d1= 0, d2 = 0; 
	  string s; 
	  s+= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	  s+= "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
	  for (int i = c1-'A'; i<s.size(); i++) {
	  	 if(s[i]==c2)break; 
	  	 d1++; 
	  }
	  for (int i = c2-'A'; i<s.size(); i++) {
	  	 if(s[i]==c1)break; 
	  	 d2++;
	  }
	  return min(d1,d2);
}


void solve(){
	       
	       int n; cin >> n; string s; cin >> s; 
	       ll ans = 9999999999999999999999; 
	       for(int i = 0; i<=n-4; i++ ) {
	       	 ll tm = 0; 
	       	 tm += dis(s[i],'A');
	       	 tm += dis(s[i+1], 'C');
	       	 tm += dis(s[i+2], 'T');
	       	 tm += dis(s[i+3], 'G'); 
	       	 ans = min (tm, ans); 
	       }
	       cout << ans << endl; 
	       
	         
	         
	               
}
int main(){
	     
	      ios::sync_with_stdio(0);
	      cin.tie(0);cout.tie(0);
	
	      int t = 1; //cin >> t;
	      while(t--) 
	      solve(); 
	   
 return 0; 
}
 


Comments

Submit
0 Comments
More Questions

1525. Number of Good Ways to Split a String
72. Edit Distance
563. Binary Tree Tilt
1306. Jump Game III
236. Lowest Common Ancestor of a Binary Tree
790. Domino and Tromino Tiling
878. Nth Magical Number
2099. Find Subsequence of Length K With the Largest Sum
1608A - Find Array
416. Partition Equal Subset Sum
1446. Consecutive Characters
1618A - Polycarp and Sums of Subsequences
1618B - Missing Bigram
938. Range Sum of BST
147. Insertion Sort List
310. Minimum Height Trees
2110. Number of Smooth Descent Periods of a Stock
2109. Adding Spaces to a String
2108. Find First Palindromic String in the Array
394. Decode String
902. Numbers At Most N Given Digit Set
221. Maximal Square
1200. Minimum Absolute Difference
1619B - Squares and Cubes
1619A - Square String
1629B - GCD Arrays
1629A - Download More RAM
1629C - Meximum Array
1629D - Peculiar Movie Preferences
1629E - Grid Xor