Link:
Uva:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&itemid=8&category=24&page=show_ problem&problem=1828
Type: Hash table
Original title:
A language is a set of strings. And the concatenation of two languages is the set of all strings this are formed by concatenating the strings of the Secon D language at the "strings of the" the "the" the "the" the "the".
For example, if we have two language A and B such that:
A = {cat, dog, mouse}
B = {rat, bat}
The concatenation of A and B would be:
C = {Catrat, catbat, Dograt, Dogbat, Mouserat, Mousebat}
Given Two languages your task is ' only ' to count the # of strings in the concatenation of the two.
Sample input;
2
3 2
Cat
Dog
Mouse
Rat
Bat
1 1
Abc
Cab
Sample output:
Case 1:6
Case 2:1
The main effect of the topic:
There are word sets a and set B, and then there are these two sets that make up a new compound, where a is the first half of the compound, and the second part of B. Give the A,b two collection, ask how many match words can be combined?
Analysis and Summary:
This feeling is UVA 10391-compound Words's brother, quite like.
Or use hash to judge heavy. Try to join a hash table without a new word, and add 1 if you succeed.
This problem is because of the misuse of memcmp and memspy to handle strings and WA several times.
There is a very pit dad, read the word to use gets, because there may be empty lines of words ....
* * UVA 10887-concatenation of Languages * time:0.404s (UVA) * author:d_double/#include <iostr
eam> #include <cstring> #include <cstdio> #define MAXN 1503 using namespace std;
Char a[maxn][12], c[maxn*maxn][24];
const int hashsize = MAXN*MAXN;
int head[hashsize], next[hashsize], rear, ans; inline void init () {rear=1; ans=0; memset (head, 0, sizeof);
} inline int hash (char *str) {int seed=131, v=0;
while (*str) v = v*seed + (*str++);
Return (V & 0x7fffffff)%hashsize;
int Try_to_insert (int s) {int h = hash (c[s]);
int u = head[h];
while (U) {if (strcmp (C[u], c[s]) ==0) return 0;
U = next[u];
} Next[s] = Head[h];
HEAD[H] = s;
return 1;
int main () {int T, M, N, Cas=1;
scanf ("%d%*c", &t);
while (t--) {scanf ("%d%d%*c", &m,&n); for (int i=0; i<m; ++i) gets (a[i));
Init ();
Char str[12], link_word[24];
for (int j=0; j<n; ++j) {gets (str);
for (int i=0; i<m; ++i) {strcpy (Link_word, a[i]);
strcat (Link_word, str);
strcpy (C[rear], Link_word);
if (Try_to_insert (rear)) {++rear; ++ans;}
} printf ("Case%d:%d\n", cas++, ans);
return 0; }