NC24710. [USACO 2010 Jan B]DNA Sequencing
描述
So for example, 'abc' could come from pair ('axx', 'xbc'), but not from the pair ('aaa', 'bbb'). Consider three bulls and two cows with these DNA sequences: Bull 1: GTTTTTTTTTTTTTTTTTTTTTTTT Bull 2: AATTTTTTTTTTTTTTTTTTTTTTT Bull 3: GATTTTTTTTTTTTTTTTTTTTTTT Cow 1: TTTTTTTTTTTTTTTTTTTTTTTTT Cow 2: ATTTTTTTTTTTTTTTTTTTTTTTT Bull 2 and cow 1 could be the parents of cow 2: Bull 2: AATTTTTTTTTTTTTTTTTTTTTTT Cow 1: TTTTTTTTTTTTTTTTTTTTTTTTT Cow 2: ATTTTTTTTTTTTTTTTTTTTTTTT since cow 2's first letter 'A' could be from Bull 2; cow 2's second letter 'T' could come from cow 1; the remainder of the letters could come from either parent. Your goal is to create a matrix of the count of possible offspring of each pairing of bulls and cows.
输入描述
* Line 1: Two space-separated integers: M and F
* Lines 2..M+1: Line i+1 gives the DNA sequence of bull i:
* Lines M+2..M+F+1: Line j+M+1 gives the DNA sequence of cow j:
输出描述
* Lines 1..M: Line i: F space-separated integers. The jth integer is the number of bovines that could be children of the ith bull and jth cow.
示例1
输入:
2 3 TGAAAAAAAAAAAAAAAAAAAAAAA AGAAAAAAAAAAAAAAAAAAAAAAA ATAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAA TTAAAAAAAAAAAAAAAAAAAAAAA
输出:
2 1 0 0 0 2
说明:
Two bulls' DNA followed by three cows' DNAJava 解法, 执行用时: 59ms, 内存消耗: 10988K, 提交时间: 2023-07-16 22:47:30
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int M = scanner.nextInt(); int F = scanner.nextInt(); int N = M + F; String[] DNA = new String[N]; for (int i = 0; i < N; i++) { DNA[i] = scanner.next(); } for (int bull = 0; bull < M; bull++) { for (int cow = M; cow < N; cow++) { int count = 0; for (int other = 0; other < N; other++) { if (other == bull || other == cow) { continue; } boolean match = true; for (int j = 0; j < 25; j++) { if (DNA[other].charAt(j) != DNA[bull].charAt(j) && DNA[other].charAt(j) != DNA[cow].charAt(j)) { match = false; break; } } if (match) { count++; } } System.out.print(count + " "); } System.out.println(); } } }
C++14(g++5.4) 解法, 执行用时: 7ms, 内存消耗: 380K, 提交时间: 2019-11-16 21:14:46
#include<stdio.h> #include<cmath> #include<algorithm> #include<string> #include<string.h> #include<iostream> #define ll long long using namespace std; string s1[30],s2[30]; int M,F; bool odk(string x,string y,string z) { for(int i=0;i<x.size();i++) if(z[i]!=x[i]&&z[i]!=y[i]) return 0; return 1; } int f(int x,int y) { int ans=0; for(int i=1;i<=M;i++) { if(i==x) continue; if(odk(s1[x],s2[y],s1[i])) ans++; } for(int i=1;i<=F;i++) { if(i==y) continue; if(odk(s1[x],s2[y],s2[i])) ans++; } return ans; } int main() { scanf("%d%d",&M,&F); for(int i=1;i<=M;i++) cin>>s1[i]; for(int i=1;i<=F;i++) cin>>s2[i]; for(int i=1;i<=M;i++) { for(int j=1;j<=F;j++) printf("%d ",f(i,j)); printf("\n"); } }
C++11(clang++ 3.9) 解法, 执行用时: 4ms, 内存消耗: 492K, 提交时间: 2020-04-03 11:55:13
#include<bits/stdc++.h> using namespace std; string str[50]; int main(){ int n,m; cin>>n>>m; for(int i=1;i<=n;i++) cin>>str[i]; for(int i=n+1;i<=n+m;i++) cin>>str[i]; for(int i=1;i<=n;i++){ for(int j=n+1;j<=n+m;j++){ int ans=0; for(int k=1;k<=n+m;k++){ if(i==k||j==k)continue; bool flag=true; for(int kk=0;kk<25;kk++) if(str[k][kk]!=str[i][kk]&&str[k][kk]!=str[j][kk]){ flag=false; break; } ans+=flag; } cout<<ans<<" "; } cout<<"\n"; } return 0; }