CC150 - 11.2

Question:

Write a method to sort an array of strings so that all the anagrams are next to each other.

 

 1 package POJ;
 2 
 3 import java.util.Arrays;
 4 import java.util.Comparator;
 5 import java.util.Hashtable;
 6 import java.util.LinkedList;
 7 import java.util.List;
 8 
 9 public class Main {
10 
11     /**
12      * 
13      * 11.2 Write a method to sort an array of strings so that all the anagrams are next to each other.
14      * 
15      */
16     public static void main(String[] args) {
17         Main so = new Main();
18     }
19 
20     public void sort(String[] list) {
21         Hashtable<String, LinkedList<String>> ht = new Hashtable<String, LinkedList<String>>();
22         AnagramCompare ac = new AnagramCompare();
23         for (String s : list) {
24             String key = ac.sortChars(s);
25             if (!ht.contains(key)) {
26                 ht.put(key, new LinkedList<String>());
27             }
28             LinkedList<String> anagrams = ht.get(key);
29             anagrams.add(s);
30         }
31         int index = 0;
32         for (String s : ht.keySet()) {
33             LinkedList<String> tempList = ht.get(s);
34             for (String t : tempList) {
35                 list[index] = t;
36                 index++;
37             }
38         }
39     }
40 }
41 
42 class AnagramCompare implements Comparator<String> {
43     public String sortChars(String s1) {
44         char[] temp = s1.toCharArray();
45         Arrays.sort(temp);
46         return new String(temp);
47     }
48 
49     @Override
50     public int compare(String o1, String o2) {
51         // TODO Auto-generated method stub
52         return sortChars(o1).compareTo(sortChars(o2));
53     }
54 }

注意:comparator的重写方法!!!

转载于:https://www.cnblogs.com/Phoebe815/p/3923666.html

(DZB) pci@pci:/HDD1/DZB/JS-G-GAN$ python 1111.py 2025-03-07 11:51:44.467742: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 AVX512F AVX512_VNNI FMA To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags. 2025-03-07 11:51:44.585546: I tensorflow/core/util/port.cc:104] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`. 2025-03-07 11:51:45.274285: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /HDD1/DZB/TensorRT-8.2.4.2/lib:/HDD1/DZB/TensorRT-8.2.4.2/lib:/usr/local/cuda-11.2/lib64:/usr/local/cuda-11.2/lib64: 2025-03-07 11:51:45.274362: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /HDD1/DZB/TensorRT-8.2.4.2/lib:/HDD1/DZB/TensorRT-8.2.4.2/lib:/usr/local/cuda-11.2/lib64:/usr/local/cuda-11.2/lib64: 2025-03-07 11:51:45.274371: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly. [PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU'), PhysicalDevice(name='/physical_device:GPU:1', device_type='GPU'), PhysicalDevice(name='/physical_device:GPU:2', device_type='GPU'), PhysicalDevice(name='/physical_device:GPU:3', device_type='GPU')]
03-08
Command line: -release -opensource -opengl es2 -confirm-license -platform linux-g++ -device linux-rdk-x5-g++ -device-option CROSS_COMPILE=/opt/gcc-arm-11.2-2022.02-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu- --sysroot=/home/wanjuxiong/rootfs -prefix /usr/local/qt5 -nomake examples -nomake tests -v 'QMAKE_LFLAGS=-Wl,-rpath-link,/home/wanjuxiong/rootfs/lib/aarch64-linux-gnu -Wl,-rpath-link,/home/wanjuxiong/rootfs/usr/lib/aarch64-linux-gnu' loaded result for config test config.qtbase.tests.machineTuple + /opt/gcc-arm-11.2-2022.02-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu-g++ -dumpmachine > aarch64-none-linux-gnu test config.qtbase.tests.machineTuple succeeded loaded result for config test config.qtbase.tests.verifyspec + cd /home/wanjuxiong/Qt/qt-everywhere-src-5.15.2/config.tests/verifyspec && /home/wanjuxiong/Qt/qt-everywhere-src-5.15.2/qtbase/bin/qmake "CONFIG -= qt debug_and_release app_bundle lib_bundle" "CONFIG += shared warn_off console single_arch" "QMAKE_CFLAGS += --sysroot=/home/wanjuxiong/rootfs" "QMAKE_CXXFLAGS += --sysroot=/home/wanjuxiong/rootfs" "QMAKE_LFLAGS += --sysroot=/home/wanjuxiong/rootfs" 'QMAKE_LFLAGS=-Wl,-rpath-link,/home/wanjuxiong/rootfs/lib/aarch64-linux-gnu -Wl,-rpath-link,/home/wanjuxiong/rootfs/usr/lib/aarch64-linux-gnu' -early "CONFIG += cross_compile" /home/wanjuxiong/Qt/qt-everywhere-src-5.15.2/qtbase/config.tests/verifyspec + cd /home/wanjuxiong/Qt/qt-everywhere-src-5.15.2/config.tests/verifyspec && MAKEFLAGS= /usr/bin/gmake clean && MAKEFLAGS= /usr/bin/gmake > rm -f verifyspec.o > rm -f *~ core *.core > /opt/gcc-arm-11.2-2022.02-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu-g++ -c -pipe -march=armv8-a+simd --sysroot=/home/wanjuxiong/rootfs -O2 -w -fPIC -I/home/wanjuxiong/Qt/qt-everywhere-src-5.15.2/qtbase/config.tests/verifyspec -I. -I/home/wanjuxiong/rootfs/usr/include -I/home/wanjuxiong/Qt/qt-everywhere-src-5.15.2/qtbase/mkspecs/devices/linux-rdk-x5-g++ -o verifyspec.o /home/wanjuxiong/Qt/qt
03-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值