new group contact

http://www.1point3acres.com/bbs/thread-148422-1-1.html

http://www.1point3acres.com/bbs/forum.php?mod=viewthread&tid=192328&extra=page%3D1%26filter%3Dsortid%26sortid%3D311%26searchoption%255B3046%255D%255Bvalue%255D%3D2%26searchoption%255B3046%255D%255Btype%255D%3Dradio&page=1

Contact Dedup, 输入是这样的:数字是用户,字母是邮箱,有很多人有多个邮箱,找出相同的用户
  1- {x,y,z},  
  2-{x} 
  3-{a,b}
  4-{y, z}
  5-{b}
  6-{m}
  7-{t,b}. visit 1point3acres.com for more.

这样输出就是 [1,2,4,7] [3,5] [6] 总共有三个用户。
可以用UnionFind或者Connected Components的方法做

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class NewGroupContact {
	public static void main(String[] args) {

		HashMap<String/* string value */, Integer/* type */> h = new HashMap<>();
		// String[][] A = { { "John", "john@gmail.com", "john@fb.com" },
		// { "Dan", "dan@gmail.com", "+1234567" },
		// { "john123", "+5412312", "john123@skype.com" },
		// { "john1985", "+5412312", "john@fb.com" },
		// { "Dan12", "+1234567", "dan@fb.com" },
		// { "Shaohui", "+4125194763", "sguo@fb.com" }};

		String[][] A = { { "x", "y", "z" }, { "x" }, { "a", "b" }, { "y", "z" }, { "m", "a" },
				{ "Shaohui", "+4125194763", "sguo@fb.com", "x" } };

		int code = 0;
		for (int i = 0; i < A.length; i++) {
			int c = -1;
			Set<Integer> groupings = new HashSet<Integer>();
			for (int j = 0; j < A[i].length; j++) {
				String s = A[i][j];
				if (h.containsKey(s)) {
					int value = h.get(s);
					groupings.add(value);
					c = Math.max(c, value);
				}
			}
			if (c == -1) {
				c = code++;
			} else {
				groupings.remove(c);
				for (Integer k : groupings) {
					for (int m = 0; m < A[0].length; m++) {
						h.put(A[k][m], c);
					}
				}

			}
			for (int j = 0; j < A[i].length; j++) {
				String s = A[i][j];
				h.put(s, c);
			}
		}

		for (int i = 0; i < A.length; i++) {
			System.out.println(h.get(A[i][0]));
		}
		Map<Integer/* type */, Set<String>> res = new HashMap<>();
		for (Map.Entry<String, Integer> entry : h.entrySet()) {
			if (res.containsKey(entry.getValue())) {
				Set<String> set = res.get(entry.getValue());
				set.add(entry.getKey());
			} else {
				Set<String> set = new HashSet<>();
				set.add(entry.getKey());
				res.put(entry.getValue(), set);
			}
		}
		for (Set<String> set: res.values()) {
			for (String s: set) {
				System.out.print(s + " ");
			}
			System.out.println("");
		}
	}
}


public List<Contact> GetContact() { try { List<Contact> contacts = new List<Contact>(); using (var context = new V_ContactContext()) { var data = context.V_ContactEntity.Where(x => x.ContactNumber != null); //var data = context.ContactEntity.Where(x => x.ContactNumber != null).GroupBy(x => x.Id).Select(x => x.OrderByDescending(y => y.Version)).ToList().Select(x => x.FirstOrDefault()).ToList(); contacts = (from contact in data select new Contact() { Id = contact.Id, Version = contact.Version, Code = contact.ContactNumber, Gender = (Gender)contact.Gender, LocalFirstName = contact.Name_Local, LocalLastName = contact.FamilyName_Local, EnglishFirstName = contact.Name_EN, EnglishLastName = contact.FamilyName_EN, EnglishNameAbbreviation = contact.NameAbbreviation_EN, LocalNameAbbreviation = contact.NameAbbreviation_Local, RelationShip = contact.RelationShip, BusinessPhoneCode = contact.BusinessPhoneCode, BusinessPhone = contact.BusinessPhone, BusinessPhoneExtension = contact.BusinessPhoneExtension, BusinessMobile = contact.BusinessMobile, BusinessMobileCode = contact.BusinessMobileCode, BusinessFax = contact.BusinessFax, BusinessFaxCode = contact.BusinessFaxCode, BusinessEmail = contact.BusinessEmail, Note = contact.Note, //TelePhone = data.Telephone, //TelephoneExtension = data.TelephoneExtension, //MobilePhone = data.MobilePhone, //Fax = data.Fax, //Email = data.Email, //Remark = data.Remark, localJobTitle = contact.JobTitle_local, EnglishJobTitle = contact.JobTitle_En, SuperiorID = contact.SuperiorID, Source = contact.Source, Birthday = contact.Birthday, }).ToList(); return contacts; } } catch (DbEntityValidationException exception) { throw exception.InnerException; } catch (Exception exception) { throw exception.InnerException; } } 这段代码怎么优化
03-08
#include <stdio.h> #include <stdlib.h> #include <string.h> #define NAME_LEN 50 #define TEL_LEN 11 typedef struct Contact { char name[NAME_LEN]; char tel[TEL_LEN]; struct Contact* next; } Contact;Contact* create_head() { Contact* head = (Contact*)malloc(sizeof(Contact)); if (!head) { printf("Memory allocation failed.\n"); exit(1); } strcpy(head->name, ""); strcpy(head->tel, ""); head->next = NULL; return head; }void add_contact(Contact* head, const char* name, const char* tel) { Contact* new_contact = (Contact*)malloc(sizeof(Contact)); if (!new_contact) { printf("Memory allocation failed.\n"); exit(1); } strncpy(new_contact->name, name, NAME_LEN - 1); strncpy(new_contact->tel, tel, TEL_LEN - 1); new_contact->next = head->next; head->next = new_contact; }void delete_contact(Contact* head, const char* name) { Contact* current = head; while (current->next && strcmp(current->next->name, name) != 0) { current = current->next; } if (current->next) { Contact* to_delete = current->next; current->next = to_delete->next; free(to_delete); printf("Contact '%s' deleted successfully.\n", name); } else { printf("Contact '%s' not found.\n", name); } }void update_contact(Contact* head, const char* name, const char* new_tel) { Contact* current = head->next; while (current && strcmp(current->name, name) != 0) { current = current->next; } if (current) { strncpy(current->tel, new_tel, TEL_LEN - 1); printf("Contact '%s' updated successfully.\n", name); } else { printf("Contact '%s' not found.\n", name); } }void search_contact(Contact* head, const char* name) { Contact* current = head->next; while (current && strcmp(current->name, name) != 0) { current = current->next; } if (current) { printf("Found contact: %s - %s\n", current->name, current->tel); } else { printf("Contact '%s' not found.\n", name); } }void display_contacts(Contact* head) { Contact* current = head->next; if (!current) { printf("No contacts available.\n"); return; } while (current) { printf("%s - %s\n", current->name, current->tel); current = current->next; } }void clean_contacts(Contact* head) { Contact* current = head->next; while (current) { Contact* temp = current; current = current->next; free(temp); } head->next = NULL; printf("All contacts have been cleared.\n"); }int main() { Contact* head = create_head(); int choice; char name[NAME_LEN], tel[TEL_LEN]; while (1) { printf("\n通讯录管理系统\n"); printf("1. 添加联系人\n"); printf("2. 删除联系人\n"); printf("3. 修改联系人信息\n"); printf("4. 查找联系人\n"); printf("5. 显示所有联系人\n"); printf("6. 清空通讯录\n"); printf("7. 退出\n"); printf("请输入选项: "); scanf("%d", &choice); switch (choice) { case 1: printf("输入姓名: "); scanf("%s", name); printf("输入电话号码: "); scanf("%s", tel); add_contact(head, name, tel); break; case 2: printf("输入要删除的姓名: "); scanf("%s", name); delete_contact(head, name); break; case 3: printf("输入要修改的姓名: "); scanf("%s", name); printf("输入新的电话号码: "); scanf("%s", tel); update_contact(head, name, tel); break; case 4: printf("输入要查找的姓名: "); scanf("%s", name); search_contact(head, name); break; case 5: display_contacts(head); break; case 6: clean_contacts(head); break; case 7: clean_contacts(head); free(head); printf("程序已退出。\n"); return 0; default: printf("无效的选择,请重新输入。\n"); } } return 0; } 为这段代码添加一个在显示所有联系人时,显示的联系人按首字母的26个字母从小到大,从上到下的自动排序,不区分大小写。
06-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值