Hdu3427 string processing

Source: Internet
Author: User

Link: Clickomania

Clickomannia is a game. The game rule is to eliminate consecutive color blocks each time, and a new link is added to each elimination, represented by a string, ask if you can eliminate the entire string.

Algorithm analysis: a string question. I used dfs. You can first scale down the string and add a number to the continuous part of the string to save it in two arrays to speed up scanning, each current character is either a string of its own, forming a legal sequence, and then determining whether the remaining string is legal or merging with the same character of the next one, provided that they must be a legal sequence, follow this classification method to directly add dfs () + tag. The key to doing this kind of question is to find the rule or lexical that makes up these strings, and then search and tag them by category. However, the sha Zai Da Niu conclusion report says it is a range of DP. Don't understand, please wait!

 

Code

 # Include <stdio. h>
# Include <string. h>
# Define NN 155
Char str [NN];
Int mark [NN] [NN];
Int num [NN];
Char cha [NN];

Int dfs (int l, int r)
{
Int I, t;
If (l = r) {/* if there is one character left, determine whether the number of connections is greater than 1 */
If (num [l]> 1)
Return 1;
Else
Return 0;
}
If (mark [l] [r]> = 0)
Return mark [l] [r];
/* Merge the current character with any of the following identical characters, provided that they are contained in the middle
The string is valid.
*/
Char ch = cha [l];
For (I = l + 1; I <= r; I ++ ){
If (cha [I] = ch ){
If (mark [l + 1] [I-1] = dfs (l + 1, I-1 )){
T = mark [I] [r];
Mark [I] [r] =-1;
Num [I] + = num [l];
Mark [I] [r] = dfs (I, r );
Num [I]-= num [l];
If (mark [I] [r] = 1)
{
Mark [I] [r] = t;
Return 1;
}
Mark [I] [r] = t;
}
}
}
/* The current character is valid. You can directly determine whether the last part is valid.
That is, if both x and y are valid, xy is also valid.
*/
If (num [l]> 1 & (mark [l + 1] [r] = dfs (l + 1, r )))
Return 1;
Return 0;
}
Int main ()
{
Int len, time, index, I;
While (scanf ("% s", str )! = EOF ){
Len = strlen (str );
If (len = 0 ){
Puts ("solvable ");
Continue;
}

/* Reduce the string to two arrays, one for the occurrence of the character, and the other for the occurrence of the character
In this example, ABBBAACC is reduced to ABAC and 1322.
*/
Time = 1;
Index = 0;
For (I = 1; I <= len; I ++ ){
If (str [I]! = Str [I-1]) {
Cha [index] = str [I-1];
Num [index] = time;
Time = 1;
Index ++;
}
Else
Time ++;
}
Memset (mark,-1, sizeof (mark ));
If (dfs (0, index-1 ))
Puts ("solvable ");
Else
Puts ("unsolvable ");
}
Return 0;
}

Two groups of test data are included:

ABBCCBCAACCA

ABBBAACCDDBCCA

Answer:

Solvable

Solvable

 

 

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.