CF 292C Beautiful IP Addresses

Source: Internet
Author: User
Tags acos

 

Question:

Croc's first round in 2013, so it's hard to do it at night...

After a simple analysis of the meaning, we can find that if n> = 7, there must be no solution, because each value is different and must be used once. In addition, it must meet the requirements of the reply, assuming n = 7, then the minimum length is 6 + 1 + 6 = 13> 12, so n> = 7 is certainly unsolvable.

Therefore, the deep search brute-force method is used to obtain all the input strings. The number of input strings does not exceed 2*6 ^ 6 (n <= 6). In each input string, add 'to each input string '. 'To form an IP address, so the maximum number of operations is 27*2*6 ^ 6 = 2519424 (certainly far smaller than this number), and the ip address must be removed. is still the original return string, otherwise there will be duplicates and errors.

 

Code:

[Cpp]
# Include <stdlib. h>
# Include <string. h>
# Include <stdio. h>
# Include <ctype. h>
# Include <math. h>
# Include <stack>
# Include <queue>
# Include <map>
# Include <set>
# Include <vector>
# Include <string>
# Include <iostream>
# Include <algorithm>
Using namespace std;
 
# Define ll _ int64
# Define ls rt <1
# Define rs ls | 1
# Define lson l, mid, ls
# Define rson mid + 1, r, rs
# Define middle (l + r)> 1
# Define clr_all (x, c) memset (x, c, sizeof (x ))
# Define clr (x, c, n) memset (x, c, sizeof (x [0]) * (n + 1 ))
# Define eps (1e-8)
# Define MOD 1000000007.
# Define INF 0x3f3f3f
# Define PI (acos (-1.0 ))
# Pragma comment (linker, "/STACK: 102400000,102400000 ")
Template <class T> T _ max (T x, T y) {return x> y? X: y ;}
Template <class T> T _ min (T x, T y) {return x <y? X: y ;}
Template <class T> T _ abs (T x) {return (x <0 )? -X: x ;}
Template <class T> T _ mod (T x, T y) {return (x> 0 )? X % y :( (x % y) + y) % y ;}
Template <class T> void _ swap (T & x, T & y) {T t = x; x = y; y = t ;}
Template <class T> void getmax (T & x, T y) {x = (y> x )? Y: x ;}
Template <class T> void getmin (T & x, T y) {x = (x <0 | y <x )? Y: x ;}
Int TS, cas = 1;
Const int M = 100000 + 5;
Int n, a [11];
Int vis [11], cnt, tot;
Struct ip {
Int a, B, c, d;
Ip (int _ a = 0, int _ B = 0, int _ c = 0, int _ d = 0) {a = _ a, B = _ B, c = _ c, d = _ d ;}
Void show () {printf ("% d. % d \ n", a, B, c, d );}
};
Struct node {
Int a [22], len;
Void show () {for (int I = 1; I <= len; I ++) printf ("% d", a [I]); puts ("");}
} Tmp;
Vector <ip> ret;
Vector <node> pal;
 
Bool check (int & num, int len, int cur, int p ){
Num = 0;
For (int I = 1; I <= len; I ++) num * = 10, num + = pal [p]. a [cur ++];
If (num> 255 | (num! = 0 & pal [p]. a [cur-len] = 0) | (num = 0 & len! = 1 ))
Return false;
Return true;
}
 
Void addToRet (int p ){
Int I, j, k, l, cur;
Ip t;
For (I = 1; I <= 3; I ++) if (pal [p]. len-I <= 9 ){
If (pal [p]. len-I <3 | pal [p]. len <I + 3) break;
If (! Check (t. a, I, 1, p) continue;
For (j = 1; j <= 3; j ++) if (pal [p]. len-I-j <= 6 ){
If (pal [p]. len-I-j <2 | pal [p]. len <I + j + 2) break;
If (! Check (t. B, j, I + 1, p) continue;
For (k = 1; k <= 3; k ++) if (pal [p]. len-I-j-k <= 3 ){
If (pal [p]. len-I-j-k <1 | pal [p]. len <I + j + k + 1) break;
If (! Check (t. c, k, I + j + 1, p) continue;
L = pal [p]. len-I-j-k;
If (! Check (t. d, l, I + j + k + 1, p) continue;
Ret. push_back (t );
}
}
}
}
 
Void dfs (int p ){
Int I;
If (p> tot ){
If (cnt = n ){
Tmp. len = (tot <1 );
For (I = tot + 1; I <= tmp. len; I ++)
Tmp. a [I] = tmp. a [tmp. len-I + 1];
Pal. push_back (tmp );
If (tot + tot-1> = 4 ){
Tmp. len-= 1;
For (I = tot + 1; I <= tmp. len; I ++)
Tmp. a [I] = tmp. a [tmp. len-I + 1];
Pal. push_back (tmp );
}
}
Return;
}
For (I = 1; I <= n; I ++ ){
If (! Vis [I]) cnt ++;
Vis [I] ++;
Tmp. a [p] = a [I], dfs (p + 1 );
Vis [I] --;
If (! Vis [I]) cnt --;
}
}
 
Void run (){
Int I, j;
For (I = 1; I <= n; I ++)
Scanf ("% d", & a [I]);
Clr_all (vis, 0), cnt = 0;
Ret. clear (), pal. clear ();
For (tot = 2; tot <= 6; tot ++) if (tot> = n) dfs (1 );
For (I = 0; I <pal. size (); I ++) addToRet (I );
Printf ("% d \ n", ret. size ());
For (I = 0; I <ret. size (); I ++) ret [I]. show ();
}
 
Void preSof (){
}
 
Int main (){
// Freopen ("input.txt", "r", stdin );
// Freopen ("output.txt", "w", stdout );
PreSof ();
// Run ();
While ((~ Scanf ("% d", & n) run ();
// For (scanf ("% d", & TS); cas <= TS; cas ++) run ();
Return 0;
}

# Include <stdlib. h>
# Include <string. h>
# Include <stdio. h>
# Include <ctype. h>
# Include <math. h>
# Include <stack>
# Include <queue>
# Include <map>
# Include <set>
# Include <vector>
# Include <string>
# Include <iostream>
# Include <algorithm>
Using namespace std;

# Define ll _ int64
# Define ls rt <1
# Define rs ls | 1
# Define lson l, mid, ls
# Define rson mid + 1, r, rs
# Define middle (l + r)> 1
# Define clr_all (x, c) memset (x, c, sizeof (x ))
# Define clr (x, c, n) memset (x, c, sizeof (x [0]) * (n + 1 ))
# Define eps (1e-8)
# Define MOD 1000000007.
# Define INF 0x3f3f3f
# Define PI (acos (-1.0 ))
# Pragma comment (linker, "/STACK: 102400000,102400000 ")
Template <class T> T _ max (T x, T y) {return x> y? X: y ;}
Template <class T> T _ min (T x, T y) {return x <y? X: y ;}
Template <class T> T _ abs (T x) {return (x <0 )? -X: x ;}
Template <class T> T _ mod (T x, T y) {return (x> 0 )? X % y :( (x % y) + y) % y ;}
Template <class T> void _ swap (T & x, T & y) {T t = x; x = y; y = t ;}
Template <class T> void getmax (T & x, T y) {x = (y> x )? Y: x ;}
Template <class T> void getmin (T & x, T y) {x = (x <0 | y <x )? Y: x ;}
Int TS, cas = 1;
Const int M = 100000 + 5;
Int n, a [11];
Int vis [11], cnt, tot;
Struct ip {
Int a, B, c, d;
Ip (int _ a = 0, int _ B = 0, int _ c = 0, int _ d = 0) {a = _ a, B = _ B, c = _ c, d = _ d ;}
Void show () {printf ("% d. % d \ n", a, B, c, d );}
};
Struct node {
Int a [22], len;
Void show () {for (int I = 1; I <= len; I ++) printf ("% d", a [I]); puts ("");}
} Tmp;
Vector <ip> ret;
Vector <node> pal;

Bool check (int & num, int len, int cur, int p ){
Num = 0;
For (int I = 1; I <= len; I ++) num * = 10, num + = pal [p]. a [cur ++];
If (num> 255 | (num! = 0 & pal [p]. a [cur-len] = 0) | (num = 0 & len! = 1 ))
Return false;
Return true;
}

Void addToRet (int p ){
Int I, j, k, l, cur;
Ip t;
For (I = 1; I <= 3; I ++) if (pal [p]. len-I <= 9 ){
If (pal [p]. len-I <3 | pal [p]. len <I + 3) break;
If (! Check (t. a, I, 1, p) continue;
For (j = 1; j <= 3; j ++) if (pal [p]. len-I-j <= 6 ){
If (pal [p]. len-I-j <2 | pal [p]. len <I + j + 2) break;
If (! Check (t. B, j, I + 1, p) continue;
For (k = 1; k <= 3; k ++) if (pal [p]. len-I-j-k <= 3 ){
If (pal [p]. len-I-j-k <1 | pal [p]. len <I + j + k + 1) break;
If (! Check (t. c, k, I + j + 1, p) continue;
L = pal [p]. len-I-j-k;
If (! Check (t. d, l, I + j + k + 1, p) continue;
Ret. push_back (t );
}
}
}
}

Void dfs (int p ){
Int I;
If (p> tot ){
If (cnt = n ){
Tmp. len = (tot <1 );
For (I = tot + 1; I <= tmp. len; I ++)
Tmp. a [I] = tmp. a [tmp. len-I + 1];
Pal. push_back (tmp );
If (tot + tot-1> = 4 ){
Tmp. len-= 1;
For (I = tot + 1; I <= tmp. len; I ++)
Tmp. a [I] = tmp. a [tmp. len-I + 1];
Pal. push_back (tmp );
}
}
Return;
}
For (I = 1; I <= n; I ++ ){
If (! Vis [I]) cnt ++;
Vis [I] ++;
Tmp. a [p] = a [I], dfs (p + 1 );
Vis [I] --;
If (! Vis [I]) cnt --;
}
}

Void run (){
Int I, j;
For (I = 1; I <= n; I ++)
Scanf ("% d", & a [I]);
Clr_all (vis, 0), cnt = 0;
Ret. clear (), pal. clear ();
For (tot = 2; tot <= 6; tot ++) if (tot> = n) dfs (1 );
For (I = 0; I <pal. size (); I ++) addToRet (I );
Printf ("% d \ n", ret. size ());
For (I = 0; I <ret. size (); I ++) ret [I]. show ();
}

Void preSof (){
}

Int main (){
// Freopen ("input.txt", "r", stdin );
// Freopen ("output.txt", "w", stdout );
PreSof ();
// Run ();
While ((~ Scanf ("% d", & n) run ();
// For (scanf ("% d", & TS); cas <= TS; cas ++) run ();
Return 0;
}

 

Related Article

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.