Example 7-5 difficult string UVa129

Source: Internet
Author: User

1. Title Description: Click to open the link

2. Solving ideas: The problem is solved by backtracking. According to test instructions description, it is known that when enumerating the cur bits, it is only necessary to check that the string is valid after it is added, without having to check whether the string between cur is legal, because this step is already done before enumerating the cur. In addition, the output of the subject to compare pits, need to be careful.

3. Code:

#define _crt_secure_no_warnings #include <iostream> #include <algorithm> #include <string> #include <sstream> #include <set> #include <vector> #include <stack> #include <map> #include < queue> #include <deque> #include <cstdlib> #include <cstdio> #include <cstring> #include < cmath> #include <ctime> #include <functional>using namespace std; #define N 80+10int s[n];int N, l;int cnt; int dfs (int cur) {if (cnt++ = = N) {for (int i = 0; i < cur; i++) {printf ("%c", ' A ' + s[i]); if ((i + 1)% 4 = = 0) {if ((i + 1)% = = 0) cout << endl;else if (i! = cur-1) Putchar (');}} if (cur) cout << endl;printf ("%d\n", cur); return 0;//returns 0 indicating that an understanding of}for (int i = 0; i < L; i++) {s[cur] = i;int o K = 1;for (int j = 1; J * 2 <= cur + 1; + j)//enumeration suffix Length (note not the length of the entire string) {int equal = 1;for (int k = 0; k < j;k++)//check prefix and suffix is No equal, s[cur-k] is the suffix part, s[cur-k-j] is the prefix part if (s[cur-k]! = s[cur-k-j]) {equal = 0; break;} if (equal) {ok = 0; break;}} If(OK) if (!dfs (cur + 1)) return 0;//If the solution is found, exit}return 1 directly;} int main () {//freopen ("T.txt", "R", stdin), while (~SCANF ("%d%d", &n, &l) && (n| | L)) {cnt = 0;memset (s, 0, sizeof (s));d FS (0);} return 0;}

Example 7-5 difficult string UVa129

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.