冰楓論壇

 找回密碼
 立即註冊
ads_sugarbook
搜索
查看: 896|回覆: 0
打印 上一主題 下一主題

[心得] [演算法心得] Uva 729. The Hamming Distance Problem

[複製鏈接]

7

主題

0

好友

11

積分

新手上路

Rank: 1

UID
320966
帖子
146
主題
7
精華
0
積分
11
楓幣
756
威望
11
存款
0
贊助金額
0
推廣
0
GP
31
閱讀權限
10
性別
保密
在線時間
9 小時
註冊時間
2021-9-30
最後登入
2022-7-6
跳轉到指定樓層
1
發表於 2022-5-12 11:26:55 |只看該作者 |倒序瀏覽
嗨大家好~ 我是新加入論壇的萌新,之後可能會每日更新一下演算法心得,那目前會在 c/c++ 版,如果大家很熱絡的話再申請 演算法/競賽程式 版吧!

--------------------------------------------------- 正文開始 ---------------------------------------------------

題目名稱: The Hamming Distance Problem
題目網站: Uva
題目網址: https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&page=show_problem&problem=729
題目內容:

The Hamming distance between two strings of bits (binary integers) is the number of corresponding
bit positions that differ. This can be found by using XOR on corresponding bits or equivalently, by
adding corresponding bits (base 2) without a carry. For example, in the two bit strings that follow:
A 0 1 0 0 1 0 1 0 0 0
B 1 1 0 1 0 1 0 1 0 0
A XOR B = 1 0 0 1 1 1 1 1 0 0
The Hamming distance (H) between these 10-bit strings is 6, the number of 1's in the XOR string.


題目大意:

給你 N H,N 代表長度,H 代表字符串中有 H 個 1.
問你有多少種字串排列方式,並從小到大輸出。

範例輸入 #1:
1

4 2

範例輸出 #1:
0011
0101
0110
1001
1010
1100
--------------------------------------------------- 題解心得 ---------------------------------------------------

遇到全排列第一個想到使用 next_permutation() ,這是 algorithm 裡面的函式庫。
他的作用是將陣列的值找出下一個字典序排列。
而他的終止條件是 逆排序 (字典序的最後一個),所以一開始是要排序過的 (就是 0...01...1)。

AC code :
  1. #include<bits/stdc++.h>
  2. #define ll long long
  3. #define INF 0x3f3f3f3f
  4. #define MOD 1000000007
  5. #define io iOS::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  6. using namespace std;
  7. const int MXN = 200005;

  8. //int ar[MXN];

  9. void sol(){
  10.   ll ans = 0, n = 0, m = 0, k = 0;

  11.   cin >> n >> m;
  12.   string str = "";
  13.   for(int i = 0; i < n - m; ++i) str += "0";
  14.   for(int i = 0; i < m; ++i) str += "1";
  15.   do{
  16.     cout << str << '\n';
  17.   }while(next_permutation(str.begin(), str.end()));
  18. }

  19. int main(){
  20.   //io
  21.   int t = 1;
  22.   cin >> t;
  23.   while(t--){
  24.     sol();
  25.     if(t) cout << '\n';
  26.   }
  27. }
複製代碼
收藏收藏0 推0 噓0


把本文推薦給朋友或其他網站上,每次被點擊增加您在本站積分: 1骰子
複製連結並發給好友,以賺取推廣點數
簡單兩步驟,註冊、分享網址,即可獲得獎勵! 一起推廣文章換商品、賺$$
高級模式
B Color Image Link Quote Code Smilies |上傳

廣告刊登意見回饋關於我們職位招聘本站規範DMCA隱私權政策

Copyright © 2011-2024 冰楓論壇, All rights reserved

免責聲明:本網站是以即時上載留言的方式運作,本站對所有留言的真實性、完整性及立場等,不負任何法律責任。

而一切留言之言論只代表留言者個人意見,並非本網站之立場,用戶不應信賴內容,並應自行判斷內容之真實性。

小黑屋|手機版|冰楓論壇

GMT+8, 2024-4-20 00:51

回頂部