fork download
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<cstring>
  4. using namespace std;
  5. struct tree {
  6. char data;
  7. tree *left, *right;
  8. };
  9. tree* build(char *ch, int len) {//t==NULL
  10. tree *head, *p, *q;
  11. head = (tree*)malloc(sizeof(tree));
  12. head->left = head->right = NULL;
  13. p = head;
  14. for (int i = 0; i < len; i++) {
  15. q = (tree*)malloc(sizeof(tree));
  16. q->data = ch[i];
  17. q->left = q->right = NULL;
  18. if (ch[i] == '0') {
  19. p->left = q;
  20. }
  21. else p->right = q;
  22. p = q;
  23. }
  24. return head;
  25. }
  26. void getrest(int n) {//n lines
  27. char ch[110];
  28. while (n--) scanf("%s%*c", ch);
  29. }
  30. void print(tree *t,int d) {
  31. if (t == NULL) {
  32. printf("empty\n");
  33. return;
  34. }
  35. print(t->right, d + 1);
  36. for (int i = 0; i < d * 4; i++) cout << " ";
  37. printf("??????????%c\n", t->data);
  38. print(t->left, d + 1);
  39. }
  40. int main() {
  41. freopen("C:/Users/SXC/Desktop/input.txt", "r", stdin);
  42. tree *head = (tree*)malloc(sizeof(tree));
  43. head->left = head->right = NULL;
  44. int n;
  45. scanf("%d%*c", &n);
  46. for (int T = 0; T < n; T++) {
  47. char ch[110];
  48. scanf("%s%*c", ch);
  49. int len = strlen(ch);
  50. tree *p = head;
  51. bool issub = true;
  52. for (int i = 0; i < len; i++) {//检索一行的输入
  53. if (ch[i] == '0') {
  54. if (p->left == NULL) {
  55. p->left = build(ch + i, len - i)->left;//非前缀
  56. issub = false;
  57. if (p->right == NULL&&i != 0) issub = true;
  58. break;
  59. }
  60. else {
  61. p = p->left;
  62. continue;
  63. }
  64. }
  65. else {///1111
  66. if (p->right == NULL) {
  67. p->right = build(ch + i, len - i)->right;//非前缀
  68. issub = false;
  69. if (p->left == NULL&&i != 0) issub = true;
  70. break;
  71. }
  72. else {
  73. p = p->right;
  74. continue;
  75. }
  76. }
  77. }
  78. if (issub) {
  79. printf("%s\n", ch);
  80. getrest(n - T);
  81. return 0;
  82. }
  83. }
  84. print(head, 0);
  85. cout << "YES" << endl;
  86. }
Success #stdin #stdout 0s 4208KB
stdin
5
00
01
10
11
111
stdout
empty
??????????
empty
YES