fork(6) download
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<iostream>
  4. #include<algorithm>
  5. using namespace std ;
  6.  
  7.  
  8. struct hi
  9. {
  10. long pr;
  11. char s[1005];
  12. }rec[1005];
  13. int qs(const void* a, const void* b) {
  14. return ((struct hi*)a)->pr-((struct hi*)b)->pr;
  15. }
  16.  
  17. int comp(char *s2,char *s)
  18. {
  19. int i;bool h=true;
  20. // printf("ni comp fun\n");
  21. if(strlen(s)>=strlen(s2))
  22. {
  23. // printf("strlen(s2)=%d\n",strlen(s2));
  24. for(i=0;i<strlen(s2);i++)
  25. if(s2[i]!=s[i])
  26. {h=false;break;}
  27.  
  28. if(h==true)return 1;//1 means successfull
  29. }
  30. return 0;
  31. }
  32. char s2[1005];
  33. long int p[1005];
  34. int main()
  35. {
  36. int i,j,n,q,mark,len;
  37. scanf("%d",&n);
  38.  
  39. for(i=0;i<n;i++)
  40. scanf("%s %ld",rec[i].s,&rec[i].pr);
  41. qsort(rec, n, sizeof(struct hi), &qs);
  42.  
  43. //sort(&rec[0].pr,&rec[n].pr);
  44. //for(i=0;i<n;i++)
  45. //printf("%s %d\n",rec[i].s,rec[i].pr);
  46. scanf("%d",&q);
  47.  
  48. for(i=1;i<=q;i++)
  49. {
  50. scanf("%s",s2);
  51. // printf("string = %s\n",s2);
  52. bool f=false;
  53. for(j=n-1;j>=0;j--)
  54. {
  55. //printf("j = %d\n",j);
  56. if(comp(s2,rec[j].s))
  57. {printf("%s\n",rec[j].s);f=true;break;}
  58. }
  59.  
  60. if(!f)printf("NO\n");
  61. }
  62. return 0;
  63. }
  64.  
Success #stdin #stdout 0s 4028KB
stdin
4
flour-with-eggs 100
chicken-ham -10
flour-without-eggs 200
fish-with-pepper 1100
6
f
flour-with
flour-with-
c
fl
chik
stdout
fish-with-pepper
flour-without-eggs
flour-with-eggs
chicken-ham
flour-without-eggs
NO