POJ 1007_DNA Sorting

来源:互联网 发布:施工进度编制软件 编辑:程序博客网 时间:2024/05/29 14:31

  本题先求各个DNA序列的逆序数,再排序,这里是稳定排序(题目中说明了如果二者逆序数相同不改变相对位置)。So,这里用stable_sort(),not sort()。 之前一直wa,把输入方式getline(cin,string **)改为cin>>..结果AC了,that's strange !


#include <iostream>#include <algorithm>#include <string>using namespace std;struct DNA{//save the DNA sequenceschar str[55];int num;} dna[105];int unsortedness(char *p){//count the unsortedness measuresint unsortNum = 0;for (int j = 1; j < strlen(p); j++){for (int k = 0; k < j; k++){if (p[k]>p[j])unsortNum++;}}return unsortNum;}bool cmp(DNA p1, DNA p2){//a parameter of  stable_sortreturn p1.num < p2.num;}int main(){int i;int n, m;     // n length of a string ,m linescin >> n >> m;for (i = 0; i < m; i++){//input test case cin >> dna[i].str;dna[i].num = unsortedness(dna[i].str);}stable_sort(dna, dna + m, cmp);// not sort()for (i = 0; i < m;i++){//display the sorted sequencescout << dna[i].str << endl;}return 0;}


0 0