UVa 1368 DNA Consensus String
2015-09-06 14:44
405 查看
题意:给出n个长度为len的DNA序列,求一个DNA序列,使得该序列到这m个DNA序列的距离尽量短
两个等长字符串的字符不同的位置个数即为两个字符串之间的距离
思路:记录下每个位置出现次数最多的字母,再扫一遍这n个串,求出距离
// Created by Chenhongwei in 2015.
// Copyright (c) 2015 Chenhongwei. All rights reserved.
#include "iostream"
#include "cstdio"
#include "cstdlib"
#include "cstring"
#include "climits"
#include "queue"
#include "cmath"
#include "map"
#include "set"
#include "stack"
#include "vector"
#include "sstream"
#include "algorithm"
using namespace std;
const int inf=1e8;
const int maxn=1e5;
typedef long long ll;
char s[60][1100];
int main()
{
//ios::sync_with_stdio(false);
// freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
char str[1100];
int T;
scanf("%d",&T);
while(T--)
{
int n,m;
scanf("%d%d\n",&n,&m);
for(int i=1;i<=n;i++)
gets(s[i]+1);
int sum=0;
for(int j=1;j<=m;j++)
{
int tmp=0;
char ans;
map<char,int> mp;
for(int i=1;i<=n;i++)
{
mp[s[i][j]]++;
if(mp[s[i][j]]>tmp||(mp[s[i][j]]==tmp&&s[i][j]<ans))
{
tmp=mp[s[i][j]];
ans=s[i][j];
}
}
sum+=n-tmp;
printf("%c",ans);
}
printf("\n%d\n",sum);
}
return 0;
}
两个等长字符串的字符不同的位置个数即为两个字符串之间的距离
思路:记录下每个位置出现次数最多的字母,再扫一遍这n个串,求出距离
// Created by Chenhongwei in 2015.
// Copyright (c) 2015 Chenhongwei. All rights reserved.
#include "iostream"
#include "cstdio"
#include "cstdlib"
#include "cstring"
#include "climits"
#include "queue"
#include "cmath"
#include "map"
#include "set"
#include "stack"
#include "vector"
#include "sstream"
#include "algorithm"
using namespace std;
const int inf=1e8;
const int maxn=1e5;
typedef long long ll;
char s[60][1100];
int main()
{
//ios::sync_with_stdio(false);
// freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
char str[1100];
int T;
scanf("%d",&T);
while(T--)
{
int n,m;
scanf("%d%d\n",&n,&m);
for(int i=1;i<=n;i++)
gets(s[i]+1);
int sum=0;
for(int j=1;j<=m;j++)
{
int tmp=0;
char ans;
map<char,int> mp;
for(int i=1;i<=n;i++)
{
mp[s[i][j]]++;
if(mp[s[i][j]]>tmp||(mp[s[i][j]]==tmp&&s[i][j]<ans))
{
tmp=mp[s[i][j]];
ans=s[i][j];
}
}
sum+=n-tmp;
printf("%c",ans);
}
printf("\n%d\n",sum);
}
return 0;
}
相关文章推荐
- 1.10055 - Hashmat the brave warrior
- 2.10071 - Back to High School Physics
- 3.458 - The Decoder
- 4.694 - The Collatz Sequence
- 6.494 - Kindergarten Counting Game
- 7.490 - Rotating Sentences
- 8.414 - Machined Surfaces
- 9.488 - Triangle Wave
- A.457 - Linear Cellular Automata
- B.489 - Hangman Judge
- C.445 - Marvelous Mazes
- 1.10494 - If We Were a Child Again
- 2.424 - Integer Inquiry
- 3.10250 - The Other Two Trees
- 5.465 - Overflow
- 6.113 - Power of Cryptography
- 7.10161 - Ant on a Chessboard
- 8.621 - Secret Research
- 9.401 - Palindromes
- A.537 - Artificial Intelligence?