package contest.contest278;
import java.util.*;
public class D {
private Map<Integer, Integer> rootMap;
private Map<Integer, Set<Integer>> map;
private Set<Integer> visited;
private void rec(int word) {
if (visited.contains(word)) {
return;
}
visited.add(word);
int tmpWord = word;
int idx = 0;
// 替换
while (tmpWord > 0) {
int mod = tmpWord % 2;
if (mod == 1) {
// 可以替换
for (int c = 0; c < 26; c++) {
if ((word & (1 << c)) != 0) {
// 已经存在了,不能替换
continue;
}
int newWord = (word ^ (1 << idx)) | (1 << c);
if (rootMap.containsKey(newWord)) {
int root = rootMap.get(word);
rootMap.put(newWord, root);
map.get(root).add(newWord);
rec(newWord);
}
}
}
tmpWord >>>= 1;
idx++;
}
// 添加
for (int c = 0; c < 26; c++) {
if ((word & (1 << c)) != 0) {
// 已经存在无法添加
continue;
}
int newWord = word | (1 << c);
if (rootMap.containsKey(newWord)) {
int root = rootMap.get(word);
rootMap.put(newWord, root);
map.get(root).add(newWord);
rec(newWord);
}
}
// 删除
tmpWord = word;
idx = 0;
while (tmpWord > 0) {
int mod = tmpWord % 2;
if (mod == 1) {
int newWord = word ^ (1 << idx);
if (rootMap.containsKey(newWord)) {
int root = rootMap.get(word);
rootMap.put(newWord, root);
map.get(root).add(newWord);
rec(newWord);
}
}
tmpWord >>>= 1;
idx++;
}
}
private int strToInt(String word) {
int num = 0;
for (char c : word.toCharArray()) {
num += 1 << (c - 'a');
}
return num;
}
public int[] groupStrings(String[] words) {
rootMap = new HashMap<>();
map = new HashMap<>();
Map<Integer, Integer> countMap = new HashMap<>();
int[] numWords = new int[words.length];
for (int i = 0; i < numWords.length; i++) {
numWords[i] = strToInt(words[i]);
}
for (int word: numWords) {
countMap.put(word, countMap.getOrDefault(word, 0) + 1);
rootMap.put(word, word);
if (!map.containsKey(word)) {
map.put(word, new HashSet<>());
}
map.get(word).add(word);
}
visited = new HashSet<>();
for (int word: numWords) {
rec(word);
}
int[] ansArr = new int[2];
Set<Integer> rootSet = new HashSet<>();
for (int word : numWords) {
rootSet.add(rootMap.get(word));
}
ansArr[0] = rootSet.size();
int maxCount = 0;
for (int root: map.keySet()) {
int count = 0;
for (int value : map.get(root)) {
count += countMap.get(value);
}
maxCount = Math.max(maxCount, count);
}
ansArr[1] = maxCount;
return ansArr;
}
public static void main(String[] args) {
// ["web","a","te","hsx","v","k","a","roh"]
// int[] ansArr = new D().groupStrings(new String[]{"a","b","ab","cde"});
// System.out.println(ansArr[0] + ", " + ansArr[1]);
// ["eifd","gzbixlp","bqdofcmaw","cgmoxwj","dy","u","qpacbn","qgls","e","bhge","ceq","nhuldtaczf","bhmsdjixyg","kzvnm","vomf","tqudmebkcw","hjvgno","fi","edaoigzqrl","j","mu","tplejbhv","zdthejsn","c","qvljkswhgi","rsuakqtcie","sxoq","u","lygpmbnvz","uzcgap","wtyd","izfh","obezv","qzrut","rvjn","mjfkyvoz","kdtmhovi","rsbxwt","siwozbnvfq","vukngxcrp","ylazmoj","gfwzhce","fbeclghqty","dojrwikhx","xtf","qvl","gd","gyfzmvl","zlypkn","eqg","wnqg","zpyl","db","kncyqp","duhwxmfzn","xzk","htopdbqc","gxj","mhircotdw","hufntl","cphkymzu","dbsiw","n","gl","eyicdomt","oyvagucb","kmdisywt","ucv","esz","ab","e","wgfekic","us","tnlpexrsqi","z","bcdvtpmq","nldqjubhga","mvcdlwkb","dxwevhanis","k","fzysliojnx","bskaep","hdjrexzku","lg","n","nhaiq","vjfhnry","gkut","dfgoye","dgw","moxlwsvdi","zlam","tulae","cpqbfinwd","fngqjerp","saitnbfcu","xcr","un","mnb","mitbk","skvayftmld","vyzoftsp","azuxfcd","oyfzrubj","rlodinph","ewj","esjg","mal","txovyj","yxmswporen","hbsmfpenvr","v","keuvmxlqab","inb","rgxpltq","hw","lqi","jnogyuvfi","ojdpc","wo","iubk","vywgsxpcbj","u","ulvdfmnrgb","bgcqwjtxn","xqlhmu","bm","myiesrg","qs","dlcyk","p","qpa","jzxv","vbgfhu","ghvcfdrjoy","j","qtycrb","pvtm","daclwtiqf","hmrdta","h","ycw","uzgk","fspdrwnc","ohsvdb","qtzxbs","p","fir","cqmsil","nwlmyafd","fqog","artjgwl","vumgskjrwf","ozwvmaqgb","lnzgrxsh","qovbaj","igxftjsm","vbxdj","ohklrtei","ugpmqefytx","cztbulnfjw","xrwhkp","aytue","vqd","fcjeho","ondbczhirp","gvwfnlqzcu","fb","mdnoslbe","lcyefn","emnizwv","ldbip","xcpqlo","dnc","jnb","h","dlqzgxeosf","wgcies","xdgynjuhpq","gawkeqifh","sbxidor","muip","svcpdfime","sm","asrwkf","suhdtk","srx","jofnzybrk","kjqoycr","womn","hljvxw","bftc","yedgc","vwukandxg","x","shnybt","thkdyfo","zibt","s","kdez","sk","kafxduohb","lwvf","lwpa","fp","wvprnjdysg","tgksohi","f","dqlaokp","fzqixc","mvac","dyiszjnfwr","k","lmg","hedpbxgny","ovt","ghpudl","w","rw","fxuaw","bu","auetn","gcdbuxjtr","skwxop","z","yctqbkvlg","mwtjsfcb","ufhy","motdnivfcr","klc","wifas","kihjls","mtlzov","fqv","ravitgjofq","gxlufbijr","eptjbycaik","jnglsihvak","tkgmhpdl","lc","qbfdp","yijbgdhvpk","jvmhlbxpye","faydcrt","nwlmifjph","fseijgwca","pxnrzk","fosiklhdgq","pnwkqtf","ekojmwd","lruwfpg","knjdqcspe","u","hmt","ouqcef","hizdgbopwr","eor","fdksphmnb","de","f","dotkm","wb","ftrxh","opjtz","uw","mrwfxksv","ijlc","vregf","nf","g","goaetbykw","pqkxbm","imhxblzqf","txk","fba","vklbds","rhsjwk","b","loxhrwvbkj","bwscmeht","quazt","oetbvzcr","ahqt","pictgn","bgjszkhxv","xgope","k","tud","dlh","thkiqgw","cnzelwp","hrxtzcbgnv","ymfwitx","owx","mwvqitnzh","w","u","umstvp","bixgnpad","nqj","iyeljwots","w","sxcbtopqen","mqalt","pgwokbi","lkewjb","gc","zqhyn","rihany","roipn","mbrjugywcd","dgiftczsb","bk","uwl","yuzlkf","vsbzfquagh","cozbwp","ygbcipw","pintymh","ks","bdzymg","krzydanft","d","hrvabm","u","lbhdxyej","bumtgy","qjukzdxo","c","hyiugascxd","kyoxzgcms","ds","euabxydjp","ovznahf","snit","pcjfandk","yonu","fbjpwva","qkcz","u","md","lek","pdqjzngrc","gphxnlvwe","gsp","yhcqnl","qkuryfsca","lfsidcrtvo","m","va","psnok","xqnstjgw","lrxbnc","v","euxnmklfpc","pcnbyivj","wqbce","tligecqj","ei","mijerqtyzv","daxszhjlq","ud","yzuxwg","hbcdzsvnmg","ofjz","eajyxn","vadtj","rulq","nea","yusd","vgiaesf","jyroizmns","cn","rju","ksyev","hfuwqvxnms","zhq","ehwyjoaru","kfz","n","bhz","aopfcvwbu","mcl","vawcteoxp","jdt","xcbi","oielcuywfn","ynphzeoi","gwa","yhofdl","slxgnuk","lcgmv","sfxjgt","hno","nsmiepz","n","ms","nfy","gplyzrscjb","jzne","vhrceplntg","cjxolnfkb","ejg","x","gdpnt","bhrnilgsj","w","f","tcgbmjle","hfradmuv","yu","xvpoywdb","ivgkplc","yhze","gkawbsuxz","fejtql","vhse","cu","c","jymr","xbwio","oqdnlusih","bhqi","flcjdkgeaw","dhxarioj","nify","rphwoqk","ingrsowjkm","bowf","olcejxn","gatsqfnl","lpkvwxdo","hv","jzdxns","znk","g","s","zegauyjw","f","c","yurdb","wqr","bezqkjrf","idjwhlv","szemt","gf","rcszejagm","maz","ndtjukb","rldzf","stdrpeoukh","ectnjw","svupnji","gptjouhbm","pt","kvgrajyn","tkxsdmne","gw","rzcktfqvl","bncvrzts","w","kmrwyobz","kzcjnvh","mhjieks","rxytd","conqe","ykrtz","nyuifckzld","ryiodvj","utahjckxg","kdh","crqwsltz","ugtdco","kxcv","cmfynlbzj","uozsdwkmyh","gpozrd","pitfyhmskx","duy","eyifghrmnb","auwhbtpqx","e","r","jivf",