String CompareTime
limit:2000/1000 MS (java/others) Memory limit:65535/32768 K (java/others)
Total submission (s): 3124 Accepted Submission (s): 740
Problem Descriptionmaybe There is 750,000 words in 中文版 and some words is prefix of other words, for example:the wor D "ACM" can be treat as one prefix of "ACMICPC". What's more, the most of such pairs of words has relationship between them. Now give your a dictionary, your work is to tell me how many such pairs.
There may is other characters in the word, for example ' _ ', '-', and so on.
Pay attention this ' a ' and ' a ' is not the same character!
Inputin the first line of the input file there was an Integer T, which means the number of test cases. Followed by T test cases.
For each test case, in the first line there are an integer n (0<n<=50000), followed n lines, each contains a word whos E length is less than, and no space appears in any word. There is no same words in and different lines.
Outputfor Each test case, tell me how many such pairs. If the number is larger than 11519, just divide it was 11519 and only output the remainder.
Sample Input
22acmicpcacm3aabcab
Sample Output
13
This problem was defeated in the time-out, the first is to use vector,string, it is clear that the use of CIN, the result has expired
Later, the use of CIN, the result is still timed out, the reason is that since it is a good order, then once the mismatch, the following string, do not need to compare the
The result, do not time out, changed WA, and what is going on?
If the number is larger than 11519, just divide it was 11519 and only output the remainder.
The problem is above, should not direct ans%11519 ... After a good reading of the problem
#include <iostream> #include <stdio.h> #include <string.h> #include <string> #include < Vector> #include <algorithm>using namespace std;typedef struct node{char a[50];} Node;node s[50010];void Swap (node *a,node *b) {char t[50]; strcpy (T,a->a); strcpy (A->a,b->a); strcpy (b->a,t);} int partition (node a[],int l,int h) {int i=l; int j=h+1; Node V=a[l]; while (true) {while (strcmp (A[++I].A,V.A) <0) if (i==h) break; while (strcmp (A[--J].A,V.A) >0) if (j==l) break; if (i>=j) break; Swap (&a[i],&a[j]); } swap (&a[l],&a[j]);//i the position is larger than partition, J is the small return j;} void Quick_sort (node a[],int l,int h) {if (h<=l) return; int j= partition (A,L,H); Quick_sort (A, L, j-1); Quick_sort (A, j+1, h);} BOOL CMP (node A1,node b1) {return strcmp (a1.a,b1.a) <0;} int main (int argc, char *argv[]) {//freopen ("1894.in", "R", stdin); int T; int N; scanf ("%d", &t); int ans=0; int Len; int k; while (t--) {ans=0; scanf ("%d", &n); for (int i=0;i<n;++i) {scanf ("%s", s[i].a); }//sort (S,S+N,CMP); Quick_sort (S, 0, N-1); for (int i=0;i<n;++i) {Len=strlen (S[I].A); for (int j=i+1;j<n;++j) {for (k=0;k<len;++k) {if (s[i].a[ K]!=S[J].A[K]) break; } if (K==len) ans++; Else break;//cannot match} if (ans>11519) ans=ans%11519; printf ("%d\n", ans); } return 0;}
Compare the difference between the quick and the STL sort
Write for yourself:
STL's:
HDU 1894 String Compare (sort)