poj3007Organize Your Train part II

http://poj.org/problem?id=3007

静态的trie树 静cz提醒 把初始化改了改 不TLE了 分8种情况讨论 有一种就是与源串相同放在最后处理就行

View Code
  1 #include <iostream>

  2 #include<cstdio>

  3 #include<cstring>

  4 #include<stdlib.h>

  5 #include<algorithm>

  6 using namespace std;

  7 struct node

  8 {

  9     int cout;

 10     int next[27];

 11 }tr[100011];

 12 int num,co,k;

 13 /*void nod()

 14 {

 15     tr[num].cout = 0;

 16     memset(tr[num].next,0,sizeof(tr[num].next));

 17 }*/

 18 void creat(char *ss)

 19 {

 20     int d,j,x=0;

 21     for(j = 0 ; j < k ; j++)

 22     {

 23         d = ss[j]-'a';

 24         if(tr[x].next[d]==0)

 25         {

 26             num++;

 27             tr[x].next[d] = num;

 28         }

 29         x = tr[x].next[d];

 30     }

 31     if(tr[x].cout==0)

 32     {

 33         co++;

 34         tr[x].cout = 1;

 35     }

 36 }

 37 int main()

 38 {

 39     int i,j,m,d,g;

 40     char ss[100],str[100];

 41     scanf("%d",&m);

 42     while(m--)

 43     {

 44         scanf("%s",str);

 45         for(i = 0 ; i <= num ; i++)

 46         {

 47             for(j = 0 ; j < 26 ; j++)

 48             tr[i].next[j] = 0;

 49             tr[i].cout = 0;

 50         }

 51         num = 0;co = 0;

 52         k = strlen(str);

 53         for(i = 1 ; i < k ; i++)

 54         {

 55             g=0;

 56             for(j = 0 ; j < i ;j++)

 57                 ss[g++] = str[j];

 58             for(j = k-1 ; j >= i ; j--)

 59                 ss[g++] = str[j];

 60             ss[g] = '\0';

 61             creat(ss);g=0;

 62             for(j = i ; j < k ; j++)

 63                 ss[g++] = str[j];

 64             for(j = 0 ; j < i ;j++)

 65                 ss[g++] = str[j];

 66             ss[g] = '\0';

 67             creat(ss);g=0;

 68             for(j = i ; j < k ; j++)

 69                 ss[g++] = str[j];

 70             for(j = i-1 ; j >= 0 ;j--)

 71                 ss[g++] = str[j];

 72             ss[g] = '\0';

 73             creat(ss);g=0;

 74             for(j = i-1 ; j >=0 ;j--)

 75                 ss[g++] = str[j];

 76             for(j = k-1 ; j >= i ; j--)

 77                 ss[g++] = str[j];

 78             ss[g] = '\0';

 79             creat(ss);g=0;

 80             for(j = i-1 ; j >=0 ;j--)

 81                 ss[g++] = str[j];

 82             for(j = i ; j < k ; j++)

 83                 ss[g++] = str[j];

 84             ss[g] = '\0';

 85             creat(ss);g=0;

 86             for(j = k-1 ; j >= i ; j--)

 87                 ss[g++] = str[j];

 88             for(j = i-1 ; j >=0 ;j--)

 89                 ss[g++] = str[j];

 90             ss[g] = '\0';

 91             creat(ss);g=0;

 92             for(j = k-1 ; j >= i ; j--)

 93                 ss[g++] = str[j];

 94             for(j = 0 ; j < i  ;j++)

 95                 ss[g++] = str[j];

 96             ss[g] = '\0';

 97             creat(ss);

 98         }

 99         creat(str);

100         printf("%d\n",co);

101     }

102     return 0;

103 }

 

 

你可能感兴趣的:(part)