标签:
http://acm.hdu.edu.cn/showproblem.php?pid=2609
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1707 Accepted Submission(s): 693
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> using namespace std; const int maxn = 205; char s1[maxn], s[maxn], a[maxn]; struct node { struct node *next[2]; }; int GetMin(char S[], int N) { int i=0, j=1; while(i<N && j<N) { int k = 0; while(S[i+k]==S[j+k] && k<N) k++; if(k==N) break; if(S[i+k]<S[j+k]) { if(j+k>i) j += k+1; else j = i+1; } else { if(i+k>j) i += k+1; else i = j+1; } } return min(i, j); } int FindTree(node *head, char s[]) { node *p=head; int i, flag = 0; for(i=0; s[i]; i++) { int k = s[i]-‘0‘; if(p->next[k]==NULL) { flag = 1; p->next[k] = new node(); } p = p->next[k]; } return flag; } void Free(node *head) { node *p = head; for(int i=0; i<2; i++) if(p->next[i]!=NULL) Free(p->next[i]); free(p); } int main() { int n; while(scanf("%d", &n)!=EOF) { int sum = 0; node *head = new node(); while(n--) { scanf("%s", s1); int len = strlen(s1); strcpy(a, s1); strcat(a, s1); int index = GetMin(a, len); strncpy(s, a+index, len); int ans = FindTree(head, s); if(ans) sum ++; } printf("%d\n", sum); Free(head); } return 0; }
标签:
原文地址:http://www.cnblogs.com/YY56/p/4846735.html