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
6
0 1 2 9 8 7
Output
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
Input
1
4
Output
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

Source

 
解题:此题不仅繁琐,而且蛋疼!过滤规则约束难搞。。。。
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#define LL long long
#define INF 0x3f3f3f
using namespace std;
int bit[],n,tot,cnt;
bool vis[];
char temp[][],s[];
char ans[][],ss[];
void dfs(int cur,int deep,int kind) {
if(cur >= deep) {
if(kind == n) {
s[cur] = '\0';
strcpy(temp[cnt],s);
cnt++;
}
return;
}
for(int i = ; i < n; i++) {
s[cur] = bit[i]+'';
if(vis[i]) {
dfs(cur+,deep,kind);
} else {
vis[i] = true;
dfs(cur+,deep,kind+);
vis[i] = false;
}
}
}
void dfs2(char *s,int cur,int p,int len) {
int i,j,k,sum = ,slen = strlen(s);
if(cur == ) {
if(p < slen) {
for(i = p; i < slen; i++)
sum = sum*+s[i]-'';
if(sum > ) return;
if(s[p] == '' && slen-p > ) return;
ss[len] = '.';
for(i = p; i < slen; i++)
ss[len++i-p] = s[i];
ss[len++i-p] = '\0';
strcpy(ans[tot++],ss+);
}
return;
}
for(i = p; i < slen; i++) {
sum = sum* + s[i]-'';
if(sum > ) break;
ss[len] = '.';
if(s[p] == '' && i-p > ) break;
for(j = p; j <= i; j++)
ss[len++j-p] = s[j];
dfs2(s,cur+,i+,len++j-p);
}
}
void go(int index) {
char s[];
int i,j,len;
strcpy(s,temp[index]);
len = strlen(temp[index]);
for(i = len-,j = len; i >= ; i--,j++)
s[j] = s[i];
s[j] = '\0';
dfs2(s,,,);
for(i = len-,j = len; i >= ; i--,j++)
s[j] = s[i];
s[j] = '\0';
dfs2(s,,,);
}
int main() {
int i;
while(~scanf("%d",&n)) {
for(i = ; i < n; i++)
scanf("%d",bit+i);
tot = cnt = ;
memset(vis,false,sizeof(vis));
for(i = ; i <= ; i++) dfs(,i,);
for(i = ; i < cnt; i++) go(i);
printf("%d\n",tot);
for(i = ; i < tot; i++)
printf("%s\n",ans[i]);
}
return ;
}

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

  1. 292C Beautiful IP Addresses

    传送门 题目 The problem uses a simplified TCP/IP address model, please read the statement carefully. An I ...

  2. 【leetcode】Restore IP Addresses

    Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...

  3. 保留ip: Reserved IP addresses

    Reserved IP addresses From Wikipedia, the free encyclopedia     In the Internet addressing architect ...

  4. 93. Restore IP Addresses

    题目: Given a string containing only digits, restore it by returning all possible valid IP address com ...

  5. 93.Restore IP Addresses(M)

    93.Restore IP Addresses Medium 617237FavoriteShare Given a string containing only digits, restore it ...

  6. 亚马逊 AWS ip反向解析:Configurable Reverse DNS for Amazon EC2’s Elastic IP Addresses

    I’d like to call your attention to a new feature that we rolled out earlier this month. You can now ...

  7. "cni0" already has an IP address different from 10.244.2.1/24。 Error while adding to cni network: failed to allocate for range 0: no IP addresses available in range set: 10.244.2.1-10.244.2.254

    "cni0" already has an IP address different from 10.244.2.1/24. Error while adding to cni n ...

  8. IP addresses in C#

    在.Net网络库里面最大的优点就是IP地址和端口被成对处理,相比于UNIX中用的方法真是一个巨大的令人欢迎的进步..NET定义了两个类来处理关于IP地址的问题. One of the biggest ...

  9. 【LeetCode】93. Restore IP Addresses

    Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...

随机推荐

  1. ROS学习笔记一:ROS安装与测试

    1 Ubuntu和ROS版本的对应关系 Ubuntu 和 ROS 都存在不同的版本,其对应关系如下: 注:如果Ubuntu版本和ROS版本不对应的话,安装就不会成功了- 笔者安装的是Ubuntu14. ...

  2. Resources.getSystem() 与 getResources()区别

    参考: http://stackoverflow.com/questions/8633539/resources-getsystem-vs-getresources 相同: 都是取得 Resource ...

  3. js点击修改按钮后修改

    <button id="click">改变内容</button> <div id="t">要改变的内容</div> ...

  4. Windows API函数大全一

    1. API之网络函数             WNetAddConnection 创建同一个网络资源的永久性连接             WNetAddConnection2 创建同一个网络资源的连 ...

  5. 面相切面编程AOP以及在Unity中的实现

    一.AOP概念 AOP(Aspect-Oriented Programming,面向切面的编程),它是可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术.它是 ...

  6. 原创 SqlParameter 事务 批量数据插入

    不错,很好,以后防注入批量事务提交虽然麻烦点研究了几个小时,但不会是问题了 SqlCommand cmd; HelpSqlServer helps = new HelpSqlServer(); //定 ...

  7. AJPFX关于异常和file类的总结

    /** * 各位坛友注意啦!对我这个帖子有任何的疑惑的,可以尽管留帖提问,我会在看到的第一时间回贴,既然写得出这帖子,* 就要对看这帖子的人负责,所以有问题,尽管问!* * * 这块没学好的同学,可以 ...

  8. [BZOJ1025][SCOI2009]游戏 DP+置换群

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1025 题目中的排数就是多少次回到原来的序列.很显然对于题目所描述的任意一种对应法则,其中一 ...

  9. install nginx error

    the error info : the HTTP gzip module requires the zlib library.You can either disable the module by ...

  10. Java 字符串格式化 String.format() 的使用

    常规类型的格式化 String类的format()方法用于创建格式化的字符串以及连接多个字符串对象.熟悉C语言的同学应该记得c语言的sprintf()方法,两者有类似之处.format()方法有两种重 ...