SHELL22. 处理文本
描述
111:13443222:13211111:13643333:12341222:12123
[111]1344313643[222]1321112123[333]12341
Bash 解法, 执行用时: 2ms, 内存消耗: 512KB, 提交时间: 2021-12-20
# awk -F ":" '{ # res[$1] = (res[$1] == "" ? $2 : (res[$1] "\n" $2)) # }END{ # for(k in res){ # print "["k"]" # print res[k] # } # }' declare -A map while read line do arr=(${line/:/ }) map["${arr[0]}"]="${map["${arr[0]}"]}${arr[1]}\n" done < nowcoder.txt k=0 for i in ${!map[*]} do [ $k -eq 0 ] && k=1 && tmp="[$i]\n${map[$i]}" && continue printf "[$i]\n${map[$i]}" done printf "$tmp"
Bash 解法, 执行用时: 3ms, 内存消耗: 400KB, 提交时间: 2021-11-25
declare -A map while read line do arr=(${line/:/ }) map["${arr[0]}"]="${map["${arr[0]}"]}${arr[1]}\n" done < nowcoder.txt k=0 for i in ${!map[*]} do [ $k -eq 0 ] && k=1 && tmp="[$i]\n${map[$i]}" && continue printf "[$i]\n${map[$i]}" done printf "$tmp"
Bash 解法, 执行用时: 3ms, 内存消耗: 408KB, 提交时间: 2022-04-13
#!/bin/bash #!/bin/awk k=0; declare -A map while read line do arr=(${line/:/ }) map["${arr[0]}"]="${map["${arr[0]}"]}${arr[1]}\n" done<nowcoder.txt for i in ${!map[*]} do [ $k -eq 0 ] && k=1 && tmp="[$i]\n${map[$i]}" && continue printf "[$i]\n${map[$i]}" done printf "$tmp"
Bash 解法, 执行用时: 3ms, 内存消耗: 420KB, 提交时间: 2022-02-14
#! /bin/bash declare -A map while read line do arr=(${line/:/ }) map["${arr[0]}"]="${map["${arr[0]}"]}${arr[1]}\n" done k=0 for i in ${!map[*]} do [ $k -eq 0 ] && k=1 && tmp="[$i]\n${map[$i]}" && continue printf "[$i]\n${map[$i]}" done printf "$tmp"
Bash 解法, 执行用时: 3ms, 内存消耗: 424KB, 提交时间: 2022-03-02
# awk -F ":" '{ # res[$1] = (res[$1] == "" ? $2 : (res[$1] "\n" $2)) # }END{ # for(k in res){ # print "["k"]" # print res[k] # } # }' declare -A map while read line do arr=(${line/:/ }) map["${arr[0]}"]="${map["${arr[0]}"]}${arr[1]}\n" done < nowcoder.txt k=0 for i in ${!map[*]} do [ $k -eq 0 ] && k=1 && tmp="[$i]\n${map[$i]}" && continue printf "[$i]\n${map[$i]}" done printf "$tmp"