### Saratov State University :: Online Contester

::Go
- home
- news
- register
 - update personal info
 - problemset archive
- submit
- status online
- standing
- contests
- virtual contests
- forum
- statistic
- FAQ
- projects

 ::Poll Are you registered on Codeforces?YesNoWhat is it???[results]

::webboard

 why "Wrong answer on test 1"??
Author: SkyCode
ID: 059354
Problem: 325
Contest: 0
Date: 2013-08-30 10:56:57

But it's working on my computer! :( I don't understand, HOW it can be(

#include <stdio.h>
#include <malloc.h>

struct spisok
{
struct spisok *next, *prev;
char c;
};

struct spisok *first=NULL, *last = NULL, *other = NULL, *ui=NULL, *uj=NULL, *uk=NULL, *ul=NULL;
int main()
{
int n = 0, symbol, swap = 0,i,j,k,l;
FILE *f;

f=fopen("input.txt", "r");

while((symbol=fgetc(f)) != EOF)
{
if (first==NULL)
{
first = (struct spisok*)malloc(sizeof(struct spisok));
first->next = NULL;
first->prev = NULL;
first->c = symbol;
last = first;
}
else
{
other = (struct spisok*)malloc(sizeof(struct spisok));
other->next = NULL;
other->prev = last;
last->next = other;
other->c = symbol;
last = other;
}
n++;
}

fclose(f);

for (i=0, j=n-1 , ui = first , uj = last; i<n/2+1, j>n/2-1; j--, i++, ui = ui->next, uj = uj->prev )
if (ui->c!=uj->c)
{
for (k=i+1,uk=ui->next,l=j-1,ul=uj->prev; k<j, l>i; k++,uk=uk->next, l--, ul=ul->prev)
{
if (uk->c==uj->c)
{
uk->prev->next = uk->next;
uk->next->prev = uk->prev;

uk->next = ui;
uk->prev = ui->prev;
if (ui->prev!=NULL) ui->prev->next = uk;
ui->prev = uk;
swap=swap+k-i;

ui=uk;
break;
}
if (ul->c==ui->c)
{
ul->prev->next = ul->next;
ul->next->prev = ul->prev;

ul->next = uj->next;
ul->prev = uj;
if (uj->next!=NULL) uj->next->prev = ul;
uj->next = ul;
swap=swap+j-l;

uj = ul;
break;
}
}
if ((ui->c)!=(uj->c))
{
printf("-1");
return 0;
}
}

printf("%d", swap);

while (first!=NULL)
{
other = first;
first=first->next;
free(other);
}

return 0;
}