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. [Usaco2005 Nov]Asteroids

    Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape o ...

  2. 51nod 1098 最小方差

    #include <iostream> #include <cstdio> #include <algorithm> using namespace std; ty ...

  3. Activity状态图、生命周期图(超详细),onSaveInstanceState只保存、恢复基本ui数据,持久数据不在这里保存。

    1.Activity状态图 2.Activity生命周期简图 启动Activity: onCreate()—>onStart()—>onResume(),Activity进入running ...

  4. github下载下来的C#控制台小游戏[含源码]

    早就听说了github是世界最大的源码库,但自己却不是很懂,今天去研究了下,注册了一个帐号,然后在上面搜索了一下C# game,然后发现有许多的游戏. 随意地选择了一个,感觉比较简单,于是就下载了下来 ...

  5. [转]Monkey测试简介

    转自:http://www.cnblogs.com/manuosex/p/3215270.html 在android手机上做自动化测试,monkey比cts,Android UnitTest 好用多了 ...

  6. 在Paint事件中绘制控件(边框)

    单纯的自己记录,将来会继续添加,侥幸被大家发现了的话请不要太鄙视... private void panel4_Paint(object sender, PaintEventArgs e) { Con ...

  7. [转]C#委托Action、Action<T>、Func<T>、Predicate<T>

    CLR环境中给我们内置了几个常用委托Action. Action<T>.Func<T>.Predicate<T>,一般我们要用到委托的时候,尽量不要自己再定义一 个 ...

  8. WPF学习10:基于MVVM Light 制作图形编辑工具(1)

    图形编辑器的功能如下图所示: 除了MVVM Light 框架是一个新东西之外,本文所涉及内容之前的WPF学习0-9基本都有相关介绍. 本节中,将搭建编辑器的界面,搭建MVVM Light 框架的使用环 ...

  9. AJPFX解析Java关键字之assert

    Java有许多关键字,但是这个关键字估计很少有人了解,今天就跟大家谈一谈这个关键字吧. 先说明一下,这个关键字开发中用的极少,感兴趣的朋友可以了解一下. 一.概述 在C和C++语言中都有assert关 ...

  10. kafaka

    http://www.360doc.com/content/15/0429/12/9350055_466788393.shtml 一.Kafka中的核心概念 Producer: 特指消息的生产者 Co ...