请你开发一个美团商家测试系统,并用等价划分法确认商家注册信息是否成功。
商家信息必须满足以下条件:
1. 系统中第一次注册的商家名字,被视为主店。
2. 系统中若出现重名商家,需要判断地址是否已存在该商家。若存在,则注册失败。否则注册成功,该商家被视为分店。
3. 商家的名字和地址必须由小写的英文字母组成,否则注册失败。
请你输出每个商家的信息,按商家名字的字典序升序输出。需要输出商家名字,商家主店地址,商家分店数量。
第一行输入一个正整数,代表注册信息数量。
接下来的行,每行输入两个字符串,用空格隔开。分别代表商家名字和商家地址。
给定的商家名字和商家地址字符串长度不超过 20,且不包含空格。
按商家名字字典序输出全部商家信息。每行输出一个,分别输出商家名字,商家主店地址,商家分店数量,用空格隔开。
5 ranko mt ranko op ranko op Ranko ok red ok
ranko mt 1 red ok 0
// 用TreeMap和LinkedHashSet import java.util.Scanner; import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); String str = in.nextLine(); Map<String,Set<String>> map = new TreeMap<>(); for(int i=0;i<n;i++){ String[] meg = in.nextLine().split(" "); String meg0 = meg[0].toLowerCase(); String meg1 = meg[1].toLowerCase(); if(meg0.equals(meg[0])&&meg1.equals(meg[1])){ Set<String> addr = map.getOrDefault(meg0,new LinkedHashSet<>()); addr.add(meg1); map.put(meg0,addr); } } for(Map.Entry<String,Set<String>> entry:map.entrySet()){ System.out.println(entry.getKey()+" "+entry.getValue().iterator().next()+" "+(entry.getValue().size()-1)); } } }
import java.util.Scanner; import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); HashMap<String, String> map = new HashMap<>(); HashMap<String, Integer> fen = new HashMap<>(); HashMap<String, HashSet<String>> used = new HashMap<>(); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int t = in.nextInt(); in.nextLine(); for(int i = 0 ; i < t; i++){ String[] str = in.nextLine().split(" "); if(!isValue(str[0]) || !isValue(str[1])) continue; if(map.containsKey(str[0])){ String key = map.get(str[0]); if(key.equals(str[1])) continue; if(used.containsKey(str[0]) && used.get(str[0]).contains(str[1])){ //System.out.println(key + " " + str[1]); continue; } int value = fen.get(key) + 1; if(used.containsKey(str[0])){ used.get(str[0]).add(str[1]); }else{ HashSet<String> set = new HashSet<>(); set.add(str[1]); used.put(str[0], set); } fen.put(key, value); }else{ map.put(str[0], str[1]); fen.put(str[1], 0); } } Map<String, String> treeMap = new TreeMap<>(map); for (String key : treeMap.keySet()) { System.out.println(key + " " + treeMap.get(key) + " " + fen.get(treeMap.get(key))); } } } public static boolean isValue(String str){ int len = str.length(); for(int i = 0; i < len; i++){ if(str.charAt(i) > 'z' || str.charAt(i) < 'a') return false; } return true; } }幽默哈希Map
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); HashMap<String, String> info = new HashMap<>(); int length = in.nextInt(); in.nextLine(); for (int i = 0; i < length; i++) { String[] line = in.nextLine().split(" "); if (Character.isLowerCase(line[0].charAt(0))) { if (!info.containsKey(line[0])) { info.put(line[0], line[1] + " " + 0); } else { String[] values = info.get(line[0]).split(" "); //System.out.print(values[0] + " " + line[1] + " "); boolean flag = false; for (int j = 0; j < values.length - 1; j++) { if (values[j].equals(line[1])) { flag = true; break; } } if (flag == false) { int count = Integer.valueOf(values[values.length - 1]) + 1; String str = ""; for (int k = 0; k < values.length - 1; k++) { str += values[k] + " "; } info.put(line[0], str + line[1] + " " + count); } } } } List<String> keys = new ArrayList<>(info.keySet()); // 对键列表按字典序升序排序 Collections.sort(keys); // 遍历排序后的键列表,输出键值对 for (String key : keys) { String value = info.get(key); String[] values = info.get(key).split(" "); System.out.println(key + " " + values[0] + " " + values[values.length - 1]); } } }
#Python n = int(input()) dict = {} name_loc_set = set() for _ in range(n): name, loc = input().split() if name != name.lower()&nbs***bsp;loc != loc.lower()&nbs***bsp;name + " " + loc in name_loc_set: continue name_loc_set.add(name + " " + loc) if name not in dict.keys(): dict[name] = [loc, 0] else: dict[name][1] += 1 # print(name_loc_set) for name, (loc, cnt) in sorted(dict.items(), key=lambda x: x[0]): print(name + " " + loc + " " + str(cnt))
#include <iostream> #include<map> #include<set> using namespace std; bool nameTest(string& temp) { for(char& c:temp) { if(c<'a'||c>'z') { return false; } } return true; } int main() { /* 思路:接受数据,map存储即可 */ //输入数据数目 int n; cin>>n; //<商家名称,<首地址,地址集合>> map< string, pair<string,set<string>> > data; string name,addr; for(int i = 0; i < n; i++) { cin>>name>>addr; //检测名称合法性 if( nameTest(name) ) { if(data[name].first=="") data[name].first = addr; //插入 data[name].second.insert(addr); } } for(auto& temp:data) { cout<<temp.first<<" "<<temp.second.first<<" "<<temp.second.second.size()-1<<endl; } return 0; } // 64 位输出请用 printf("%lld")
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); TreeMap<String, Shop> m = new TreeMap<>(); for (int i = 0; i < n; ++i) { String name = in.next(), pos = in.next(); if (!name.toLowerCase().equals(name)) continue; if (!m.containsKey(name)) m.put(name, new Shop(pos)); else m.get(name).poses.add(pos); } for (String name: m.keySet()) { System.out.printf("%s %s %d\n", name, m.get(name).main_pos, m.get(name).poses.size() - 1); } } } class Shop { String main_pos; Set<String> poses; Shop(String main_pos) { this.main_pos = main_pos; this.poses = new HashSet<>(); poses.add(main_pos); } }
a = int(input()) c = {} for i in range(a): b = input().split() if any(char.isupper() for char in b[0]): continue if b[0] not in c.keys(): c[b[0]] = [] c[b[0]].append(b[1]) elif b[0] in c.keys() and b[1] not in c[b[0]]: c[b[0]].append(b[1]) c = dict(sorted(c.items())) for key in c.keys(): count = len(c[key]) - 1 print(key + ' ' + c[key][0] + ' ' + str(count))
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); String[] Str1 = new String[n]; String[] Str2 = new String[n]; Map<String, List<String>> map1 = new TreeMap<>(); Map<String, String> map2 = new TreeMap<>(); Map<String, Integer> map3 = new TreeMap<>(); for (int i = 0; i < n; i++) { Str1[i] = in.next(); Str2[i] = in.next(); } for (int i = 0; i < n; i++) { String str1 = Str1[i]; String str2 = Str2[i]; if ((str1.charAt(0) >= 'a' && str1.charAt(0) <= 'z') && (str2.charAt(0) >= 'a' && str2.charAt(0) <= 'z')) { if (!map1.containsKey(str1)) { List<String> list = new ArrayList<>(); list.add(str2); map1.put(str1, list); map2.put(str1, str2); map3.put(str1, 1); continue; } if (map1.containsKey(str1)) { if (!map1.get(str1).contains(str2)) { List<String> list = map1.get(str1); list.add(str2); map1.put(str1, list); int count = map3.get(str1); map3.put(str1, count + 1); } } } } Set<String> set1 = map1.keySet(); Set<String> set2 = map2.keySet(); Set<String> set3 = map3.keySet(); Iterator<String> it1 = set1.iterator(); Iterator<String> it2 = set2.iterator(); Iterator<String> it3 = set3.iterator(); while (it1.hasNext()) { String str1 = it1.next(); System.out.print(str1 + " "); String str2 = it2.next(); System.out.print(map2.get(str2) + " "); String str3 = it3.next(); System.out.println(map3.get(str3) - 1); } } }
#include <iostream> #include<algorithm> #include<map> #include<string> #include<bits/stdc++.h> using namespace std; struct shop { int id; string name; string address; map<string,int>add; int other; }; int main() { //int a, b; int n; cin>> n; int i=0; int k=0; map<string,int>d; string s1,s2; int flag=1; vector<shop>shopt; while (i<n) { // 注意 while 处理多个 case cin>>s1>>s2; flag=1; for(int j=0;j<s1.length();j++) { if(isupper(s1[j])) { flag=0; break; } } if(flag!=0) { auto temp= d.find(s1); if(temp==d.end()) { d.insert(make_pair(s1,k)); shop shoptemp; shoptemp.id = k; shoptemp.address = s2; shoptemp.name = s1; shoptemp.other=0; shoptemp.add.insert(make_pair(s2,1)); k++; shopt.push_back(shoptemp); } else { //cout<<s2<<endl; // cout<<d[s1]<<endl; auto temp= d.find(s1); int tt = temp->second; auto temp2 = shopt[tt].add.find(s2); if(temp2==shopt[tt].add.end()) { shopt[tt].other++; //cout<<shopt[tt].other<<endl; shopt[tt].add.insert(make_pair(s2,1)); } } } i++; } auto it = d.begin(); //cout<<shopt.size()<<endl; for(;it!=d.end();it++) { cout<<it->first<<" "<<shopt[it->second].address<<" "<<shopt[it->second].other<<endl; } } // 64 位输出请用 printf("%lld")
n = int(input()) mydict = {} for i in range(n): name, pos = input().split() flag = True for j in name: if j < 'a'&nbs***bsp;j > 'z': flag = False break if flag: for k in pos: if k < 'a'&nbs***bsp;k > 'z': flag = False break if flag: if name not in mydict.keys(): mydict[name] = [[pos],0] elif pos not in mydict[name][0]: mydict[name][1] += 1 mydict[name][0].append(pos) name = sorted(mydict.keys()) for i in name: print(i, mydict[i][0][0], mydict[i][1])
// Golang代码实现 // 通过切片字典记录出现的店铺名称和对应的地址,如果出现相同的地址,则不计入,如果不同则为分店,如果没有店铺名则是主店,切片字典第一个即为主店 package main import ( "fmt" "sort" ) func main() { var m int // 存储对应店面和对应主店和分店地址,第一个为主店,其他为分店 dt := map[string][]string{} // 用来存储并遍历店铺名称,按字典序排列 tgname := []string{} fmt.Scanf("%d", &m) for i := 0; i < m; i++ { var name, add string fmt.Scanln(&name, &add) // 如果输入的店铺和地址不为小写则不计入,结束后续操作 if !pdown(name) || !pdown(add) { continue } // 判断是否已经存在 _, ok := dt[name] if !ok { // 若不存在,则加入店铺和对应地址,以及店铺名在切片中的信息 dt[name] = append(dt[name], add) tgname = append(tgname, name) } else { // 若已经存在 则判断新的店铺地址是否和已经存在的店铺地址冲突 var tag bool = true for _, v := range dt[name] { if add == v { // 如果冲突则不加入 tag = false break } } if tag { // 如果不冲突则加入这个新地址 dt[name] = append(dt[name], add) } } } // 对店铺名进行字典序排列 sort.Strings(tgname) // 输出对应信息即可 for _, v := range tgname { fmt.Println(v, dt[v][0], le***])-1) } } // 用来判断是否都是小写字母 func pdown(nums string) bool { for _, v := range nums { if v < 'a' || v > 'z' { return false } } return true }
#include <cctype> #include <iostream> #include <map> #include <string> #include <vector> using namespace std; bool check(string str){ for(int i=0;i<str.size();i++){ if(!islower(str[i])) return false; } return true; } int main() { int n; map<string,vector<string>> business; vector<string> exist; cin >> n; for (int i = 0; i < n; i++) { string name, address; cin >> name >> address; if(check(name) && check(address)){ bool flag=true; for(int i=0;i<exist.size();i++){ if(!exist[i].compare(name+address)) flag=false; } if(flag){ business[name].push_back(address); exist.push_back(name+address); } } } for(auto i=business.begin();i!=business.end();i++) cout<<i->first<<" "<<i->second[0]<<" "<<i->second.size()-1<<endl; } // 64 位输出请用 printf("%lld")