HDU 4545 magic string

Source: Internet
Author: User

Magic string
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission (s): 203 Accepted Submission (s): 87

 

Problem Description
Tom and his good friend Tom are playing a new game. Tom gives a string consisting of lower-case letters, and James gives a string longer than Tom, which is also composed of lower-case letters, if Xiao Ming's string and Xiao Xi can be converted into the same one through magic conversion, both of them will be very happy. Here, magic means that James's string can be used to delete a character at will, or to change some characters against character changes in the table. For example:
The string of xiaoxi is abba;
Xiaoming's string is addba;
Character Change table d B (meaning that d can be converted to B ).
Then James can delete the first d and convert the second d to B to convert the string to abba.

Now, please help us determine: Can they convert the strings of two people to the same through magic conversion?
 


Input
Input T to indicate that there is a total of T group test data (T <= 40 ).
Next we will have a total of T groups of data. The first line of each group of data is the string of xiaoxi, and the second line is the string of James (the data guarantee that the string length cannot exceed 1000, xiaoming's string must be greater than or equal to xiaoxi, and all characters are lowercase letters ). Then input the alphabet, input m first, which indicates that there are m character conversion methods (m <= 100), and then input two lower-case letters in each line of m, indicates that the previous one can be changed to the next one (but it does not mean that the last one can be changed to the previous one ).
 


Output
For each group of data, the number of cases is output first.
If the string of two people can be converted to the same by magic conversion, the output is "happy ",
Otherwise, "unhappy" is output ".
Each group of data occupies one row. For the specific output format, see the sample.
 


Sample Input
2
Abba
Addba
1
D B
A
Dd
0


Sample Output
Case #1: happy
Case #2: unhappy


Source
2013 Jinshan xishanju creative game program challenge-Preliminary Round (1)
 


Recommend
Liuyiding
Solution: simply get greedy and write down the variable letters of each letter in string B. In string a, as long as a letter in string B and its variable letters can match it, the reason for this is that you can only delete letters from string B, that is, you must ensure that the matching letters of string a in string B are incremented in sequence. Therefore, at the end of the match, as long as all the letters in string a are matched, they are happy. Otherwise, they are unhappy.
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Include <string>
# Include <vector>
Using namespace std;
Int main ()
{
Int I, j, k, t, l1, l2, m, p, q;
Char s1 [1001], s2 [1001], t1 [2], t2 [2];
Bool visit [1001], flag;
Scanf ("% d", & t );
Getchar ();
Vector <char> s [26];
For (p = 1; p <= t; p ++)
{
Q = 0;
For (I = 0; I <26; I ++)
S [I]. clear ();
Memset (s1, '\ 0', sizeof (s1 ));
Memset (s2, '\ 0', sizeof (s2 ));
Memset (visit, false, sizeof (visit ));
Scanf ("% s", s1, s2 );
L1 = strlen (s1); l2 = strlen (s2 );
S1 [l1] = '\ 0'; s2 [l2] =' \ 0 ';
Scanf ("% d", & m );
For (I = 0; I <l1; I ++)
S [s1 [I]-'a']. push_back (s1 [I]);
While (m --)
{
Scanf ("% s", t1, t2 );
K = s [t1 [0]-'a']. size ();
S [t1 [0]-'a']. push_back (t2 [0]);
}
For (I = 0; I <l1; I ++)
{
Flag = false;
For (j = q; j <l2; j ++)
{
If (! Visit [j])
{
For (k = 0; k <s [s2 [j]-'a']. size (); k ++)
If (s [s2 [j]-'a'] [k] = s1 [I])
{
Visit [j] = true;
Flag = true;
Q = j;
Break;
}
}
If (flag)
Break;
}
If (! Flag)
Break;
}
Printf ("Case # % d:", p );
If (flag)
Printf ("happy \ n ");
Else
Printf ("unhappy \ n ");
}
Return 0;
}

# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Include <string>
# Include <vector>
Using namespace std;
Int main ()
{
Int I, j, k, t, l1, l2, m, p, q;
Char s1 [1001], s2 [1001], t1 [2], t2 [2];
Bool visit [1001], flag;
Scanf ("% d", & t );
Getchar ();
Vector <char> s [26];
For (p = 1; p <= t; p ++)
{
Q = 0;
For (I = 0; I <26; I ++)
S [I]. clear ();
Memset (s1, '\ 0', sizeof (s1 ));
Memset (s2, '\ 0', sizeof (s2 ));
Memset (visit, false, sizeof (visit ));
Scanf ("% s", s1, s2 );
L1 = strlen (s1); l2 = strlen (s2 );
S1 [l1] = '\ 0'; s2 [l2] =' \ 0 ';
Scanf ("% d", & m );
For (I = 0; I <l1; I ++)
S [s1 [I]-'a']. push_back (s1 [I]);
While (m --)
{
Scanf ("% s", t1, t2 );
K = s [t1 [0]-'a']. size ();
S [t1 [0]-'a']. push_back (t2 [0]);
}
For (I = 0; I <l1; I ++)
{
Flag = false;
For (j = q; j <l2; j ++)
{
If (! Visit [j])
{
For (k = 0; k <s [s2 [j]-'a']. size (); k ++)
If (s [s2 [j]-'a'] [k] = s1 [I])
{
Visit [j] = true;
Flag = true;
Q = j;
Break;
}
}
If (flag)
Break;
}
If (! Flag)
Break;
}
Printf ("Case # % d:", p );
If (flag)
Printf ("happy \ n ");
Else
Printf ("unhappy \ n ");
}
Return 0;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.