xtu summer individual-4 A - Beautiful IP Addresses
Beautiful IP Addresses
This problem will be judged on CodeForces. Original ID: 292C
64-bit integer IO format: %I64d Java class name: (Any)
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:
- write out in a line four 8-bit numbers of the IP address, without the commas;
- 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
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
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的更多相关文章
- 292C Beautiful IP Addresses
传送门 题目 The problem uses a simplified TCP/IP address model, please read the statement carefully. An I ...
- 【leetcode】Restore IP Addresses
Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...
- 保留ip: Reserved IP addresses
Reserved IP addresses From Wikipedia, the free encyclopedia In the Internet addressing architect ...
- 93. Restore IP Addresses
题目: Given a string containing only digits, restore it by returning all possible valid IP address com ...
- 93.Restore IP Addresses(M)
93.Restore IP Addresses Medium 617237FavoriteShare Given a string containing only digits, restore it ...
- 亚马逊 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 ...
- "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 ...
- IP addresses in C#
在.Net网络库里面最大的优点就是IP地址和端口被成对处理,相比于UNIX中用的方法真是一个巨大的令人欢迎的进步..NET定义了两个类来处理关于IP地址的问题. One of the biggest ...
- 【LeetCode】93. Restore IP Addresses
Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...
随机推荐
- 《linux就该这么学》学习笔记
本篇文章是根据刘遄老师的<linux就该这么学>中个人易忘知识点的读书笔记,结合的是个人弱点,可能不适合广大的网友同学,并在此声明本篇文章只是用于学习之用,绝无侵犯版权之意 linux就该 ...
- 背包DP HDOJ 5410 CRB and His Birthday
题目传送门 题意:有n个商店,有m金钱,一个商店买x件商品需要x*w[i]的金钱,得到a[i] * x + b[i]件商品(x > 0),问最多能买到多少件商品 01背包+完全背包:首先x == ...
- 尺取法 POJ 3601 Subsequence
题目传送门 /* 题意:求连续子序列的和不小于s的长度的最小值 尺取法:对数组保存一组下标(起点,终点),使用两端点得到答案 1. 记录前i项的总和,求[i, p)长度的最小值,用二分找到sum[p] ...
- taskkill帮助信息
taskkill帮助信息: C:\Users\xusweeter>taskkill /? TASKKILL [/S system [/U username [/P [password]]]] { ...
- SimpleDateForma类
package Format_daqo; import java.text.SimpleDateFormat; import java.util.Date; public class SimpleDa ...
- jmeter(十七)逻辑控制器
JMeter中的Logic Controller用于为Test Plan中的节点添加逻辑控制器. JMeter中的Logic Controller分为两类:一类用来控制Test Plan执行过程中节点 ...
- suricata.yaml (一款高性能的网络IDS、IPS和网络安全监控引擎)默认配置文件(图文详解)
不多说,直接上干货! 前期博客 基于CentOS6.5下Suricata(一款高性能的网络IDS.IPS和网络安全监控引擎)的搭建(图文详解)(博主推荐) 或者 基于Ubuntu14.04下Suric ...
- Zclip点击复制内容到剪贴板兼容各浏览器
WEB开发中,要让用户复制页面中的一段代码.URL地址等信息,为了避免用户拖动鼠标再进行右键复制操作而可能出现的差错,我们可以直接在页面中放置一个复制按钮,只需要轻轻一点这个复制按钮,内容将会被复制, ...
- AJPFX关于网络编程的理解
1:网络编程(理解) (1)网络编程:用Java语言实现计算机间数据的信息传递和资源共享 (2)网络编程模型 (3)网络编程的三要素 ...
- Android应用开发细节点
1.如果handler是在主线程声明,就属于主线程,handleMessage属于引用handler的那个线程:2.ByteArrayOutputStream/ByteArrayInputStream ...