HRBUST2030 (dfs), hrbust2030dfs

Source: Internet
Author: User

HRBUST2030 (dfs), hrbust2030dfs
Idiom Solitaire

Time Limit: 1000 MS Memory Limit: 32768 KB

64-bit integer IO format: % lld, % llu Java class name: Main

[Submit] [Status] [Discuss]

Question link: http://acm.hrbust.edu.cn/vj/index.php? C = problem-problem & id = 131971

Description provides N idioms. The number of idioms connected to the longest dragon is obtained through the number of idioms connected to the dragon.
Each idiom consists of at least three to eight Chinese characters. If the last word of the previous idiom is the same as the first word of the next one, it can be connected together.
To simplify the problem, each Chinese character is replaced by a four-letter code. Ensure that each Chinese character has a unique encoding. All letters are lowercase letters and start with the first idiom. Each idiom can only be used once.

Input Multiple groups of test data, for each group of data
The first line is an integer N, which represents N idioms.
Next N rows, each row has an idiom.
(N <= 20)
Maximum Output length: Sample Input
5
adfkejimejlsgkeh
emiemkwlcuhelmge
gkeheohowehiemie
lmgejoewijfeabcd
emiekejlwejdadfk
Sample Output
4
 
 
Solution:
This question is a Kan · first note that the first four letters and the last four letters of each string should be saved, then note that the first idiom is used to start every time the dragon is connected.
After reading the question, you can start to consider solving it. The question requires the maximum output length. It is obvious that the first word can be used to obtain the third word, and then I can connect to the second word at this time. This is a kind of backtracking. So we use the first word as the starting point and use dfs to search n-1 strings.
Here, I learned a good idea from LSJ. I thought of it as a tree and finally sought the deepest height. Use vis to mark each search. The two principles are as follows: (1) Mark I don't leave (2) point that is the same as the current key value I don't leave.
Max_cnt is updated each time. Note the difference between ++ step and 1 + step in recursion. If it is written as ++ step, the value of step is changed, and the node height at the same layer is changed. If it is 1 + step, the value of step is not changed. After scanning a node, you can access other nodes in the same layer based on the original step value.
 
Complete code:
#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <sstream>#include <iomanip>#include <numeric>#include <cstring>#include <climits>#include <cassert>#include <complex>#include <cstdio>#include <string>#include <vector>#include <bitset>#include <queue>#include <stack>#include <cmath>#include <ctime>#include <list>#include <set>#include <map>using namespace std;#pragma comment(linker, "/STACK:102400000,102400000")typedef long long LL;typedef double DB;typedef unsigned uint;typedef unsigned long long uLL;/** Constant List .. **/ //{const int MOD = int(1e9)+7;const int INF = 0x3f3f3f3f;const LL INFF = 0x3f3f3f3f3f3f3f3fLL;const DB EPS = 1e-9;const DB OO = 1e20;const DB PI = acos(-1.0); //M_PI;string str;int n;int vis[100001];struct node{    string start;    string ends;}q[100001];int max_cnt;int dfs(int key , int step , int &max_cnt){    for(int i = 0 ; i < n ; i ++)    {        if(vis[i] == 0 && i != key)        {            if(q[key].ends == q[i].start)            {                vis[i] = 1;                int t = dfs(i , 1+step , max_cnt);                vis[i] = 0;                max_cnt = max(max_cnt , t);            }        }    }    return step;}int main(){    #ifdef DoubleQ    freopen("in.txt","r",stdin);    #endif    while(~scanf("%d",&n))    {        for(int i = 0 ; i < n ; i ++)        {            cin >> str ;            int len  = str.length();            q[i].start = "";            q[i].start += str[0];            q[i].start += str[1];            q[i].start += str[2];            q[i].start += str[3];            q[i].ends = "";            q[i].ends += str[len-4];            q[i].ends += str[len-3];            q[i].ends += str[len-2];            q[i].ends += str[len-1];        }        memset(vis , 0 , sizeof(vis));        max_cnt = 0;        vis[0] = 1;        int step = 0;        dfs(max_cnt , step , max_cnt);        printf("%d\n",max_cnt+1);    }}



What are the advantages of DFS and DFS?

Through DFS, a sharing point on a server can act as the host of shared resources residing on other servers. DFS transparently links the file server and shared folders and maps them to a single hierarchy so that they can be accessed from one location, while data is actually distributed in different locations. You do not have to go to multiple locations on the network to find the required information. Instead, you only need to connect:
\ DfsServer \ Dfsroot

When a user accesses a folder in the shared folder, the user is redirected to a network location that contains the shared resource. In this way, you only need to know that the DFS root directory is shared to access the shared resources of the entire enterprise.

The DFS topology starts from the root directory of the DFS tree. The DFS root directory at the top of the logical hierarchy maps to a physical share. The DFS link maps the Domain Name System (DNS) name to the UNC name of the destination shared folder or destination DFS root directory. When the DFS client accesses the DFS shared folder, the DFS server maps the DNS name to the UNC name and returns the reference to the client so that it can find the shared folder. Map the DNS name to the UNC name so that the physical location of the data is transparent to the user, so that the user does not need to remember the server that stores the folder. When the DFS client requests a reference from the DFS share, the DFS server uses the Partition Table (PKT) to direct the DFS client to the physical share. For domain-based DFS, PKT is stored in Active Directory; for independent DFS, PKT is stored in the registry. In the network environment, PKT maintains all information about the DFS topology, including its ing to basic physical sharing. After the DFS server directs the DFS client to the copy sharing list corresponding to the requested DFS link, the DFS client connects to a copy of the same site using the Active Directory site topology, if the site does not provide a copy, a copy other than the site is connected.

Distributed Software Systems (Distributed Software Systems) is a Software system that supports Distributed processing and executes tasks in a multi-processor architecture interconnected by a communication network. It includes distributed operating systems, distributed programming languages, compilation (Interpretation) systems, distributed file systems, and distributed database systems.

The distributed operating system manages distributed processing system resources and controls
The types of distributed systems can be roughly classified into three types:

1. distributed data, but there is only one total? No local database.

2. Hierarchical processing. Each layer has its own database.

3. A fully-dispersed distributed network without central control. There are multiple connection methods between nodes, such as loose connection, close connection, and dynamic connection, broadcast and notification connections.

Deep priority (DFS) Algorithm

Depth precedence: O (n + e) indicates that in the graph, if the vertex v is used as the start point, we select a vertex w that has not been searched from the vertex v's adjacent list, and the vertex w continues to be searched by the depth priority method. If no vertex is found, the vertex is stored in the stack. Knowing that no vertex has been traversed by the adjacent vertex u, return to the vertex in the stack, return to the previous vertex to continue to find the vertex that has not been traversed, until all vertices have been searched. Over ~!
 

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.