Guest User

Untitled

a guest
Oct 19th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.96 KB | None | 0 0
  1.     #include <stdio.h>
  2.     #include <stdlib.h>
  3.     #include <string.h>
  4.     #include <assert.h>
  5.     #include <iostream>
  6.     #define K 1000000
  7.      
  8.     struct node {
  9.     int children[26];
  10.     char isWord;
  11.     char best[30];
  12.     char sbest[30];
  13.     };
  14.      
  15.     bool flag;
  16.      
  17.     struct node Trie[K];
  18.     int trieNodeCount;
  19.      
  20.      
  21.     using namespace std;
  22.      
  23.      
  24.     char c;
  25.      
  26.      
  27.     int initialize() {
  28.     trieNodeCount = 1;
  29.     }
  30.      
  31.     int add(char *word, int length,char *rword) {
  32.     int i, curNode = 1, nextNode;
  33.     for (i=0; i<length; i++) {
  34.     nextNode = Trie[curNode].children[ word[i] - 'a' ];
  35.     if ( nextNode == 0 ) {
  36.     trieNodeCount++;
  37.     Trie[curNode].children[ word[i] - 'a' ] = trieNodeCount;
  38.     curNode = trieNodeCount;
  39.     }
  40.     else {
  41.     curNode = nextNode;
  42.     }
  43.     // swap function
  44.     if (strcmp(Trie[curNode].best,rword)>0||!strlen(Trie[curNode].best)) {
  45.      
  46.     for (int i=0;i<strlen(Trie[curNode].best);i++) {
  47.     Trie[curNode].sbest[i]=Trie[curNode].best[i];
  48.     }
  49.     for (int i=strlen(Trie[curNode].best);i<strlen(Trie[curNode].sbest);i++)
  50.     Trie[curNode].sbest[i]=c;
  51.     for (int i=0;i<strlen(rword);i++) {
  52.     Trie[curNode].best[i]=rword[i];
  53.     }
  54.     for (int i=strlen(rword);i<strlen(Trie[curNode].best);i++)
  55.     Trie[curNode].best[i]=c;
  56.     // end of swap function
  57.     }
  58.     else
  59.     // second swap function
  60.     if (strcmp(Trie[curNode].best,rword)<=0&&(strcmp(Trie[curNode].sbest,rword)>0||!strlen(Trie[curNode].sbest))) {
  61.     for (int i=0;i<strlen(rword);i++) {
  62.     Trie[curNode].sbest[i]=rword[i];
  63.     }
  64.     for (int i=strlen(rword);i<strlen(Trie[curNode].sbest);i++)
  65.     Trie[curNode].sbest[i]=c;
  66.     }
  67.     // end of second swap function
  68.     }
  69.     Trie[curNode].isWord = 1;
  70.     }
  71.      
  72.      
  73.      
  74.     void query(char *word, int length,char *rword) {
  75.     int i, curNode = 1,ansNode=1,see;
  76.     for (i=0; i<length; i++) {
  77.     curNode = Trie[curNode].children[ word[i] - 'a' ];
  78.     if ( curNode == 0 )
  79.     break;
  80.     if (strcmp(rword,Trie[curNode].best)!=0) {
  81.     ansNode=curNode;
  82.     see=0;
  83.     }
  84.     else
  85.     if (strlen(Trie[curNode].sbest)) {
  86.     ansNode=curNode;
  87.     see=1;
  88.     }
  89.     }
  90.     if (!see)
  91.     printf("%s\n",Trie[ansNode].best);
  92.     else
  93.     printf("%s\n",Trie[ansNode].sbest);
  94.     }
  95.      
  96.      
  97.      
  98.      
  99.     int main () {
  100.     //freopen("prhyme.out","w",stdout);
  101.     // freopen("prhyme.in","r",stdin);
  102.     initialize();
  103.     while(1) {
  104.     char s[31],rs[31];
  105.     for (int i=0;i<31;i++) {
  106.     s[i]=c;
  107.     rs[i]=c;
  108.     }
  109.     gets(s);
  110.     int n=strlen(s);
  111.     if( !strcmp(s,"") )
  112.     break;
  113.     for (int i=0;i<n;i++) {
  114.     rs[i]=s[n-(i+1)];
  115.     }
  116.     add(rs,n,s);
  117.     }
  118.     while(!feof(stdin)) {
  119.     char s[31],rs[31];
  120.     for (int i=0;i<31;i++) {
  121.     s[i]=c;
  122.     rs[i]=c;
  123.     }
  124.     gets(s);
  125.     int n=strlen(s);
  126.     for (int i=0;i<n;i++) {
  127.     rs[i]=s[n-(i+1)];
  128.     }
  129.     query(rs,strlen(rs),s);
  130.     }
  131.     return 0;
  132.     }
Add Comment
Please, Sign In to add comment