UVa 10508 - Word Morphing

来源:互联网 发布:变分原理知乎 编辑:程序博客网 时间:2024/05/03 09:09

題目:有一些長度相同的字符串,從起始字符串每次改變一個字符,轉化成其他字符,求轉化順序。

分析:簡單題。直接求出不同字符的個數排序輸出即可。

說明:題目的條件都是迷惑的\(^o^)/~。

#include <algorithm>#include <iostream>#include <cstring>#include <cstdio>using namespace std;char str[1001][1001];typedef struct _mydata{int index;int value;}mydata;mydata Item[1001];int cmp(const void *p, const void *q){mydata *a = (mydata *)p;mydata *b = (mydata *)q;return a->value - b->value;}int main(){int n, m, value;while (~scanf("%d%d",&n,&m)) {for (int i = 0; i < n; ++ i) {scanf("%s",str[i]);}for (int i = 0; i < n; ++ i) {value = 0;for (int j = 0; str[i][j]; ++ j) {value += (str[i][j] != str[0][j]);}Item[i].value = value;Item[i].index = i;}qsort(Item, n, sizeof(mydata), cmp);for (int i = 0; i < n; ++ i) {printf("%s\n",str[Item[i].index]);}}return 0;}


0 0
原创粉丝点击