Deep Search ——— ZOJ 1004:anagrams by Stack

Source: Internet
Author: User

Details of the various abuses!!

Actually, it's a simple, deep search.

As a binary tree to understand: Each node has two branches: into the stack and out of the stack.

Pruning operation: only if the top element of the stack and the target character at the current position are the same, the stack will not be out.

Dfs writes three parameters: Depth search depth, Npush stack count, npop number of stacks

Npush used to record the number of stacks: The main judge is the current pressure stack is reasonable, and the element to be pressed into the original string position

Npop used to record the number of stacks: Determine the location of the generated target string elements

When npush==npop== the target string, the description generates an executable sequence of actions

Note that the output operation string must be controlled with the depth parameter, because the maximum length of the string has changed over multiple inputs, and only using depth to determine which part of the string is currently generated.

Sticker Code!

# include<iostream># include<string># include<cstdio># include<cstring>using namespacestd;stringSource;stringTarget;Charsolution[ $];Chars[ $];inttop;voidDfsintDepthintNpush,intNpop) {    if(Npush = = Target.length () && Npop = =target.length ()) {         for(inti =0; I < depth; i++) {cout<< solution[i]<<" "; } cout<<Endl; return; }    if(Npush <target.length ()) {S[top++] =Source[npush]; Solution[depth]='I'; DFS (Depth+1, Npush +1, Npop); Top--; }    if(Top >0&& S[top-1] ==Target[npop]) {Solution[depth]='o'; Chartemp = s[top-1]; Top--; DFS (Depth+1, Npush, Npop +1); S[top++] =temp; }    return;}intMain () { while(cin>>source>>target) {Top=1; cout<<"["<<Endl; if(source.length () = =target.length ()) DFS (0,0,0); cout<<"]"<<Endl; }    return 0;}

Deep Search ——— ZOJ 1004:anagrams by Stack

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.