标签:style blog http color java os io for
An IP address is a 32-bit integer, represented as a group of four decimal 8-bit integers (without leading zeroes), separated by commas. For example, record 0.255.1.123 shows a correct IP address and records 0.256.1.123 and 0.255.1.01 do not. In the given problem an arbitrary group of four 8-bit integers is a correct IP address.
Our hero Polycarpus still works as a system administrator in some large corporation. He likes beautiful IP addresses. To check if some IP address is beautiful, he should do the following:
Let us remind you that a palindrome is a string that reads the same from right to left and from left to right.
For example, IP addresses 12.102.20.121 and 0.3.14.130 are beautiful (as strings "1210220121" and "0314130" are palindromes), and IP addresses 1.20.20.1 and 100.4.4.1 are not.
Polycarpus wants to find all beautiful IP addresses that have the given set of digits. Each digit from the set must occur in the IP address at least once. IP address must not contain any other digits. Help him to cope with this difficult task.
The first line contains a single integer n (1 ≤ n ≤ 10) — the number of digits in the set. The second line contains the set of integers a1, a2, ..., an(0 ≤ ai ≤ 9). It is guaranteed that all digits in the set are distinct.
In the first line print a single integer k — the number of beautiful IP addresses that contain the given set of digits. In the following k lines print the IP addresses, one per line in the arbitrary order.
6
0 1 2 9 8 7
6
78.190.209.187
79.180.208.197
87.190.209.178
89.170.207.198
97.180.208.179
98.170.207.189
1
4
16
4.4.4.4
4.4.4.44
4.4.44.4
4.4.44.44
4.44.4.4
4.44.4.44
4.44.44.4
4.44.44.44
44.4.4.4
44.4.4.44
44.4.44.4
44.4.44.44
44.44.4.4
44.44.4.44
44.44.44.4
44.44.44.44
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <vector> 6 #include <climits> 7 #include <algorithm> 8 #include <cmath> 9 #define LL long long 10 #define INF 0x3f3f3f 11 using namespace std; 12 int bit[12],n,tot,cnt; 13 bool vis[12]; 14 char temp[10000][30],s[30]; 15 char ans[10000][60],ss[60]; 16 void dfs(int cur,int deep,int kind) { 17 if(cur >= deep) { 18 if(kind == n) { 19 s[cur] = ‘\0‘; 20 strcpy(temp[cnt],s); 21 cnt++; 22 } 23 return; 24 } 25 for(int i = 0; i < n; i++) { 26 s[cur] = bit[i]+‘0‘; 27 if(vis[i]) { 28 dfs(cur+1,deep,kind); 29 } else { 30 vis[i] = true; 31 dfs(cur+1,deep,kind+1); 32 vis[i] = false; 33 } 34 } 35 } 36 void dfs2(char *s,int cur,int p,int len) { 37 int i,j,k,sum = 0,slen = strlen(s); 38 if(cur == 3) { 39 if(p < slen) { 40 for(i = p; i < slen; i++) 41 sum = sum*10+s[i]-‘0‘; 42 if(sum > 255) return; 43 if(s[p] == ‘0‘ && slen-p > 1) return; 44 ss[len] = ‘.‘; 45 for(i = p; i < slen; i++) 46 ss[len+1+i-p] = s[i]; 47 ss[len+1+i-p] = ‘\0‘; 48 strcpy(ans[tot++],ss+1); 49 } 50 return; 51 } 52 for(i = p; i < slen; i++) { 53 sum = sum*10 + s[i]-‘0‘; 54 if(sum > 255) break; 55 ss[len] = ‘.‘; 56 if(s[p] == ‘0‘ && i-p > 0) break; 57 for(j = p; j <= i; j++) 58 ss[len+1+j-p] = s[j]; 59 dfs2(s,cur+1,i+1,len+1+j-p); 60 } 61 } 62 void go(int index) { 63 char s[60]; 64 int i,j,len; 65 strcpy(s,temp[index]); 66 len = strlen(temp[index]); 67 for(i = len-1,j = len; i >= 0; i--,j++) 68 s[j] = s[i]; 69 s[j] = ‘\0‘; 70 dfs2(s,0,0,0); 71 for(i = len-2,j = len; i >= 0; i--,j++) 72 s[j] = s[i]; 73 s[j] = ‘\0‘; 74 dfs2(s,0,0,0); 75 } 76 int main() { 77 int i; 78 while(~scanf("%d",&n)) { 79 for(i = 0; i < n; i++) 80 scanf("%d",bit+i); 81 tot = cnt = 0; 82 memset(vis,false,sizeof(vis)); 83 for(i = 2; i <= 6; i++) dfs(0,i,0); 84 for(i = 0; i < cnt; i++) go(i); 85 printf("%d\n",tot); 86 for(i = 0; i < tot; i++) 87 printf("%s\n",ans[i]); 88 } 89 return 0; 90 }
xtu summer individual-4 A - Beautiful IP Addresses,布布扣,bubuko.com
xtu summer individual-4 A - Beautiful IP Addresses
标签:style blog http color java os io for
原文地址:http://www.cnblogs.com/crackpotisback/p/3892853.html