Advertisement
Guest User

awaal

a guest
Nov 12th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define NMax 100005
  5. #define LMax 31
  6. int n, m;
  7.  
  8. typedef struct thing { // path or truck :)
  9. char cap[LMax];
  10. int idx, match;
  11. } thing;
  12. thing a[NMax], b[NMax];
  13.  
  14. int compCap(const void *a, const void *b) {
  15. char *aa = ((thing *)a)->cap;
  16. char *bb = ((thing *)b)->cap;
  17.  
  18. if (strlen(aa) > strlen(bb)) { return -1; }
  19. if (strlen(aa) < strlen(bb)) { return 1; }
  20.  
  21. for (int i = 0; i < strlen(aa); i++) {
  22. if (aa[i] > bb[i]) { return -1; }
  23. if (aa[i] < bb[i]) { return 1; }
  24. }
  25.  
  26. return 0;
  27. }
  28. int compIdx(const void *a, const void *b) {
  29. return ((thing *)a)->idx - ((thing *)b)->idx;
  30. }
  31.  
  32. int main() {
  33. freopen("camioane.in", "rt", stdin);
  34. freopen("camioane.out", "wt", stdout);
  35.  
  36. scanf("%d %d", &n, &m);
  37. for (int i = 0; i < n; i++) {
  38. scanf("%s", a[i].cap);
  39. a[i].idx = i + 1;
  40. }
  41. for (int i = 0; i < m; i++) {
  42. scanf("%s", b[i].cap);
  43. b[i].idx = i + 1;
  44. }
  45. qsort(a, n, sizeof(a[0]), compCap);
  46. qsort(b, m, sizeof(b[0]), compCap);
  47.  
  48. int cnt = 0, j = 0;
  49. for (int i = 0; i < n; i++) {
  50. while (compCap(&a[i], &b[j]) == 1 && j < m) { j++; }
  51. if (j >= m) { break; }
  52. a[i].match = b[j].idx;
  53. j++;
  54. cnt++;
  55. }
  56. qsort(a, n, sizeof(a[0]), compIdx);
  57. printf("%d\n", cnt);
  58. for (int i = 0; i < n; i++) {
  59. printf("%d ", a[i].match);
  60. }
  61. printf("\n");
  62. fclose(stdout);
  63. return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement