xtu summer individual-4 A - Beautiful IP Addresses

Beautiful IP Addresses

Time Limit: 2000ms

Memory Limit: 262144KB

This problem will be judged on CodeForces. Original ID: 292C
64-bit integer IO format: %I64d      Java class name: (Any)

The problem uses a simplified TCP/IP address model, please read the statement carefully.

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:

  1. write out in a line four 8-bit numbers of the IP address, without the commas;
  2. check if the resulting string is a palindrome.

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.

Input

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.

Output

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.

Sample Input

Input

60 1 2 9 8 7

Output

678.190.209.18779.180.208.19787.190.209.17889.170.207.19897.180.208.17998.170.207.189

Input

14

Output

164.4.4.44.4.4.444.4.44.44.4.44.444.44.4.44.44.4.444.44.44.44.44.44.4444.4.4.444.4.4.4444.4.44.444.4.44.4444.44.4.444.44.4.4444.44.44.444.44.44.44

Source

Croc Champ 2013 - Round 1

解题:此题不仅繁琐,而且蛋疼!过滤规则约束难搞。。。。

 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

时间: 2024-08-24 11:42:37

xtu summer individual-4 A - Beautiful IP Addresses的相关文章

xtu summer individual 5 A - To Add or Not to Add

To Add or Not to Add Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 231C64-bit integer IO format: %I64d      Java class name: (Any) A piece of paper contains an array of n integers a1, a2, ..., an. Y

xtu summer individual 5 E - Burning Bridges

Burning Bridges Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Original ID: 258864-bit integer IO format: %lld      Java class name: Main Ferry Kingdom is a nice little country located on N islands that are connected by

leetcode -day29 Binary Tree Inorder Traversal &amp; Restore IP Addresses

1.  Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 分析:求二叉树的中序

[LeetCode]Restore IP Addresses

题目:Restore IP Addresses 将数字串转换成合法的IP地址,返回所有可能的情况. 思路: 移动3个地址分隔符".",将地址换分成四份,检查是否合法: 注意不能有0开头的地址段(它是非法的),且不用将开头的0去掉. 中间的点从第一个点的后面开始向尾部移动,当第一个点与第二个点的距离大于3时,第一个点向后移动一步,第二个点还原到当前第一个点的后面: 当第一个点移动到最后一个点的前2个位置,则,最后一个点向前移动一步,第一个点和第二个点还原: 最后一个点到串尾距离大于3则退

Leetcode题解(2):L93/Restore IP Addresses

L93: Restore IP Addresses Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example: Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does n

xtu summer individual 6 B - Number Busters

Number Busters Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 382B64-bit integer IO format: %I64d      Java class name: (Any) Arthur and Alexander are number busters. Today they've got a competition.

xtu summer individual 1 E - Palindromic Numbers

E - Palindromic Numbers Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Description A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the same when its digits are reversed. In this

93. Restore IP Addresses

Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example: Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does not matter) 什么时候临时容器符合题意加入结

xtu summer individual 6 D - Checkposts

Checkposts Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 427C64-bit integer IO format: %I64d      Java class name: (Any) Your city has n junctions. There are m one-way roads between the junctions. A