|
Your task is, given an integer N, to make a palidrome (word that
reads the same when you reverse it) of length at least N. Any palindrome will do. Easy, isn't
it? That's what you thought before you passed it on to your inexperienced
team-mate. When the contest is almost over, you find out that that problem
still isn't solved. The problem with the code is that the strings generated
are often not palindromic. There's not enough time to start again from
scratch or to debug his messy code. Seeing that the situation is desperate,
you decide to simply write some additional code that takes the output and
adds just enough extra characters to it to make it a palindrome and hope for
the best. Your solution should take as its input a string and produce the
smallest palindrome that can be formed by adding zero or more characters at
its end.
|
|
||||||
|
|
Input
|
|
|
||||
|
|
Input will consist of several lines ending in EOF. Each line will contain
a non-empty string made up of upper case and lower case English letters
('A'-'Z' and 'a'-'z'). The length of the string will be less than or equal to
100,000. |
|
|||||
|
|
|
|
|||||
|
|
Output
|
|
|||||
|
|
For each line of input, output will consist of exactly one line. It should
contain the palindrome formed by adding the fewest number of extra letters to
the end of the corresponding input string. |
|
|||||
|
|
|
|
|||||
|
|
Sample
Input
|
Sample
Output
|
|
|
|
||
|
|
aaaa
abba amanaplanacanal xyz |
aaaa
abba amanaplanacanalpanama xyzyx |
|
|
|
||
|
|
| ||||||
El grupo de Programación Competitiva Pu++ esta formado por alumnos de varias licenciaturas de la F. Ciencias (Abierto a la Comunidad de CU) con el principal objetivo de participar en concursos ACM. En este blog se encuentra algo de Teoria y Problemas con los que anteriormente hemos trabajado.
martes, 26 de marzo de 2013
Extend to Palindrome
jueves, 21 de marzo de 2013
Problema A del regional: Arranging heaps
A mining company extracts terbium, a rare metal used for constructing lightweight magnets, from
river sand. They mine the Long River at N mining points, each of them identified by its distance
from the river source. At each mining point, a relatively small but highly valued heap of mineral ore
is extracted from the river.
To collect the mineral ore, the company regroups the N produced heaps into a smaller number of K heaps, each located at one of the initial mining points. The newly formed heaps are then collected by trucks.
To regroup the N heaps, they use a barge, which in practice can carry any amount of mineral ore because it is very large. The barge starts at the river source and can only travel downriver, so the heap produced at a mining point X can be taken to a mining point Y only if Y > X. Each heap is moved completely to another mining point, or not moved at all. The cost of moving a heap of weight W from a mining point X to a mining point Y is W x (Y - X). The total cost of the regrouping is the sum of the costs for each heap movement. Notice that a heap which is not moved has no influence on the total cost.
Given the values for N and K, the N mining points, and the weight of the heap each mining point produced, write a program that calculates the minimum total cost to regroup the N initial heaps into K heaps.
K < N
1000). Each of the next N lines describes one of the initial heaps with two integers X and W
indicating that the mining point X produced a heap of weight W (
1
X, W
106). Within each test
case the heaps are given in strictly ascending order considering their mining points.
To collect the mineral ore, the company regroups the N produced heaps into a smaller number of K heaps, each located at one of the initial mining points. The newly formed heaps are then collected by trucks.
To regroup the N heaps, they use a barge, which in practice can carry any amount of mineral ore because it is very large. The barge starts at the river source and can only travel downriver, so the heap produced at a mining point X can be taken to a mining point Y only if Y > X. Each heap is moved completely to another mining point, or not moved at all. The cost of moving a heap of weight W from a mining point X to a mining point Y is W x (Y - X). The total cost of the regrouping is the sum of the costs for each heap movement. Notice that a heap which is not moved has no influence on the total cost.
Given the values for N and K, the N mining points, and the weight of the heap each mining point produced, write a program that calculates the minimum total cost to regroup the N initial heaps into K heaps.
Input
Each test case is described using several lines. The first line contains two integers N and K denoting respectively the number of initial heaps and the desired number of heaps after regrouping ( 1Sample Output
For each test case output a line with an integer representing the minimum total cost to regroup the N initial heaps into K heaps.Sample Input
3 1 20 1 30 1 40 1 3 1 11 3 12 2 13 1 6 2 10 15 12 17 16 18 18 13 30 10 32 1 6 3 10 15 12 17 16 18 18 13 30 10 32 1
Sample Output
30 8 278 86
sábado, 16 de marzo de 2013
417 UVA word index
Word Index
Encoding schemes are often used in situations requiring encryption or
information storage/transmission
economy. Here, we develop a simple encoding scheme that encodes particular
types of words with five or fewer (lower case) letters as integers.
| Word Index |
Consider the English alphabet {a,b,c,...,z}. Using this alphabet, a set of valid words are to be formed that are in a strict lexicographic order. In this set of valid words, the successive letters of a word are in a strictly ascending order; that is, later letters in a valid word are always after previous letters with respect to their positions in the alphabet list {a,b,c,...,z}. For example,
abc aep gwz
are all valid three-letter words, whereas
aab are cat
are not.
For each valid word associate an integer which gives the position of the word in the alphabetized list of words. That is:
a -> 1
b -> 2
.
.
z -> 26
ab -> 27
ac -> 28
.
.
az -> 51
bc -> 52
.
.
vwxyz -> 83681
Your program is to read a series of input lines. Each input line will have
a single word on it, that will be
from one to five letters long. For each word read, if the word is invalid
give the number 0. If the word read
is valid, give the word's position index in the above alphabetical list.
Input
The input consists of a series of single words, one per line. The words are at least one letter long and no more that five letters. Only the lower case alphabetic {a,b,...,z} characters will be used as input. The first letter of a word will appear as the first character on an input line.The input will be terminated by end-of-file.
Output
The output is a single integer, greater than or equal to zero (0) and less than or equal 83681. The first digit of an output value should be the first character on a line. There is one line of output for each input line.Sample Input
z a cat vwxyz
Sample Output
26 1 0 83681
miércoles, 13 de marzo de 2013
Solución al problema C del Regional ACM ICPC Latin American 2012
Este fue uno de los problemas más resueltos durante el concurso. Pueden encontrar la redacción y subir sus códigos en:
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=572&page=show_problem&problem=4144
Para resolverlo, una aproximación es construir el TRIE de las entradas marcando en cada nodo la cantidad de palabras que comparten el prefijo. Observamos entonces que es necesario teclear un caracter si es un caracter inicial o si en el TRIE el número de palabras que comparten el prefijo que estamos revisando es diferente al número correspondiente en el nodo padre. De este modo, podemos realizar una búsqueda en profundidad en el TRIE para contar la cantidad de teclas que es necesario presionar en total.
Una posible implementación comentada, es la siguiente:
#include<stdio.h>
long i,j,k,u,v,n;
char p[82];
double r;
struct nodo{
long pre; // Numero de palabras que tienen el prefijo
char pro; // Altura del nodo en el arbol
long sig[28]; // Posicion de los nodos hijos en el arreglo
}T[1000004];
void a(){
long in=0,no=0; // in es el indice del caracter, no es la posicion del nodo en que estamos dentro del arrelo
while(p[in]){ // Mientras no lleguemos al final de la palabra
if(T[no].sig[p[in]]){ // Si el prefijo ya esta, avanzamos a la posicion
no=T[no].sig[p[in]];
}else{ // Si no, creamos el nodo
T[no].sig[p[in]]=u;
no=u;
u++;
}
T[no].pre++; // Aumentamos el contador de palabras con el prefijo
T[no].pro=in; // Marcamos la altura del nodo
in++;
}
}
void dfs(long po, long rep){
long ii;
if(rep!=T[po].pre&&T[po].pro){ k=k+T[po].pre; } // Si cambia la cantidad de palabras con el prefijo y no estamos en el primer caracter, agrega al contador el numero de palabras
rep=T[po].pre; // Actualiza el numero de palabras con el prefijo que se esta revisando
for(ii=1;ii<28;ii++){ // Revisamos todos los nodos hijos recursivamente
if(T[po].sig[ii]){
dfs(T[po].sig[ii],rep);
}
T[po].sig[ii]=false; // Aprovechando que estamos recorriendo todo en DFS, borramos lo necesario para el siguiente caso
T[po].pre=0;
}
}
int main(){
while(scanf("%ld",&n)==1){
u=1;
for(i=0;i<n;i++){
scanf("%s",p);
j=0;
while(p[j]){ p[j]=p[j]-'a'+1; j++; } // Asignamos a las letras de la a a la z un numero del 1 al 26 para facilitar su manejo
a(); // Agregamos la palabra al TRIE
}
k=0; // Inicializamos el contador k
v=0;
dfs(0,v);
r=double(k+n)/double(n);
printf("%.2lf\n",r);
}
}
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=572&page=show_problem&problem=4144
Para resolverlo, una aproximación es construir el TRIE de las entradas marcando en cada nodo la cantidad de palabras que comparten el prefijo. Observamos entonces que es necesario teclear un caracter si es un caracter inicial o si en el TRIE el número de palabras que comparten el prefijo que estamos revisando es diferente al número correspondiente en el nodo padre. De este modo, podemos realizar una búsqueda en profundidad en el TRIE para contar la cantidad de teclas que es necesario presionar en total.
Una posible implementación comentada, es la siguiente:
#include<stdio.h>
long i,j,k,u,v,n;
char p[82];
double r;
struct nodo{
long pre; // Numero de palabras que tienen el prefijo
char pro; // Altura del nodo en el arbol
long sig[28]; // Posicion de los nodos hijos en el arreglo
}T[1000004];
void a(){
long in=0,no=0; // in es el indice del caracter, no es la posicion del nodo en que estamos dentro del arrelo
while(p[in]){ // Mientras no lleguemos al final de la palabra
if(T[no].sig[p[in]]){ // Si el prefijo ya esta, avanzamos a la posicion
no=T[no].sig[p[in]];
}else{ // Si no, creamos el nodo
T[no].sig[p[in]]=u;
no=u;
u++;
}
T[no].pre++; // Aumentamos el contador de palabras con el prefijo
T[no].pro=in; // Marcamos la altura del nodo
in++;
}
}
void dfs(long po, long rep){
long ii;
if(rep!=T[po].pre&&T[po].pro){ k=k+T[po].pre; } // Si cambia la cantidad de palabras con el prefijo y no estamos en el primer caracter, agrega al contador el numero de palabras
rep=T[po].pre; // Actualiza el numero de palabras con el prefijo que se esta revisando
for(ii=1;ii<28;ii++){ // Revisamos todos los nodos hijos recursivamente
if(T[po].sig[ii]){
dfs(T[po].sig[ii],rep);
}
T[po].sig[ii]=false; // Aprovechando que estamos recorriendo todo en DFS, borramos lo necesario para el siguiente caso
T[po].pre=0;
}
}
int main(){
while(scanf("%ld",&n)==1){
u=1;
for(i=0;i<n;i++){
scanf("%s",p);
j=0;
while(p[j]){ p[j]=p[j]-'a'+1; j++; } // Asignamos a las letras de la a a la z un numero del 1 al 26 para facilitar su manejo
a(); // Agregamos la palabra al TRIE
}
k=0; // Inicializamos el contador k
v=0;
dfs(0,v);
r=double(k+n)/double(n);
printf("%.2lf\n",r);
}
}
domingo, 10 de marzo de 2013
Funciones de lectura y escritura y el algoritmo KMP
Dejo el link a un breve resumen de algunas funciones de lectura y escritura en C/C++ (un texto muy introductorio) y a una corta reseña sobre el algoritmo Knuth-Morris-Pratt, que encuentra todas las ocurrencias de una subcadena en una cadena de caracteres en tiempo lineal sobre la longitud de la cadena. Recomiendo ampliamente resolver los problemas propuestos con el KMP, con el fin de practicar su implementación y conseguir una mejor comprensión del funcionamiento del algoritmo.
https://docs.google.com/file/d/0B6M0ckH75hxIbkRfNlpsc2M2S1k/edit?usp=sharing
https://docs.google.com/file/d/0B6M0ckH75hxIRGFpXzNkQmlocTA/edit?usp=sharing
miércoles, 6 de marzo de 2013
UVA 10298 Power Strings
Given two strings a and b we define a*b to be
their concatenation. For example, if a = "abc" and b = "def"
then a*b = "abcdef". If we think of concatenation as multiplication,
exponentiation by a non-negative integer is defined in the normal way:
a^0 = "" (the empty string) and a^(n+1) = a*(a^n).
Each test case is a line of input representing s, a string of printable characters. For each s you should print the largest n such that s = a^n for some string a. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.
Each test case is a line of input representing s, a string of printable characters. For each s you should print the largest n such that s = a^n for some string a. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.
Sample Input
abcd aaaa ababab .
Output for Sample Input
1 4 3
martes, 5 de marzo de 2013
Manejo de cadenas de caracteres
Un poco acerca del manejo de caracteres:
https://docs.google.com/file/d/0B0ZpbtK9Vuz1N1J5bW1NZHV1eTA/edit?usp=sharing
https://docs.google.com/file/d/0B0ZpbtK9Vuz1N1J5bW1NZHV1eTA/edit?usp=sharing
Suscribirse a:
Entradas (Atom)