您的位置:首页 > 其它

poj1520scramble_sort

2011-06-06 10:37 183 查看
Scramble Sort

Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 1896Accepted: 972
Description

In this problem you will be given a series of lists containing both words and numbers. The goal is to sort these lists in such a way that all words are in alphabetical order and all numbers are in numerical order. Furthermore, if the nth element in the list is a number it must remain a number, and if it is a word it must remain a word.
Input
The input will contain multiple lists, one per line. Each element of the list will be separated by a comma followed a space, and the list will be terminated by a period. The input will be terminated by a line containing only a single period.
Output
For each list in the input, output the scramble sorted list, separating each element of the list with a comma followed by a space, and ending the list with a period.
Sample Input
0.
banana, strawberry, OrAnGe.
Banana, StRaWbErRy, orange.
10, 8, 6, 4, 2, 0.
x, 30, -20, z, 1000, 1, Y.
50, 7, kitten, puppy, 2, orangutan, 52, -100, bird, worm, 7, beetle.
.

Sample Output
0.
banana, OrAnGe, strawberry.
Banana, orange, StRaWbErRy.
0, 2, 4, 6, 8, 10.
x, -20, 1, Y, 30, 1000, z.
-100, 2, beetle, bird, 7, kitten, 7, 50, orangutan, puppy, 52, worm.


// poj1028.cpp : 定义控制台应用程序的入口点。
//
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;

int flag[100];//标志原串中是数字还是单词
string word[30];//输入存储数组
int dig[30];//保存数字
string cha[30];//保存单词
char str[100];//输入存储数组
int i,j,k,len,clen,dlen;//

int input()
{
i=0;
cin>>str;//输入字符串
if(str[0]=='.')//若是一个“.”,结束
return 1;
while(str[strlen(str)-1]!='.')//考虑进行改进
{
str[strlen(str)-1]= '/0';
word[i]=str;
if(isalpha(str[0]))//判断是否是字母
flag[i]=1;
else
flag[i]=0;
i++;
cin>>str;
}
str[strlen(str)-1]='/0';
word[i]=str;
if(isalpha(str[0]))
flag[i]=1;
else
flag[i]=0;
len=i+1;//记录输入的数字和单词的总个数
return 0;
}

int atoi(string s,int i)//将ASCII码数字转换成int型
{
int n=0;

for(;i<s.length()&&s[i]>='0'&&s[i]<='9';++i)
n=10*n+(s[i]-'0');
return n;
}
void partition()//将数字和单词分开保存的函数
{
j=0,k=0,clen=0,dlen=0;
for(i=0;i<len;i++)
{
if(flag[i]==1)//字母
{
cha[j]=word[i];
j++;
clen++;//记录有多少个单词
}
else
{
if(word[i][0]!='-')
dig[k]=atoi(word[i],0);
else
{
dig[k]=(-1)*atoi(word[i],1);
}
k++;dlen++;//记录有多少个数字
}
}
}

int cmp2(string a,string b)//用于单词的比较
{
char c[30];//用于临时保存被小写化得单词(题目中要求不区分大小写)
char d[30];
for(int i=0;i<a.length();i++)
{
c[i]=isupper(a[i]) ? a[i]-'A' +'a':a[i];
}
for(int i=0;i<b.length();i++)
{
d[i]=isupper(b[i]) ? b[i]-'A' +'a':b[i];
}
if(strcmp(c,d)<=0)
return 1;
else
return 0;
}

int part1(string a[],int p,int r)//单词数组的quick_sort中的partition()函数
{
string x=a[r];
int i=p-1;
string temp;
for(int j=p;j<=r-1;j++)
{
if(cmp2(a[j],x))
{
i++;
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
temp=a[r];
a[r]=a[i+1];
a[i+1]=temp;
return i+1;
}

int part2(int a[],int p,int r)//数字数组的quick_sort中的partition()函数
{
int x=a[r];
int i=p-1;
int temp;
for(int j=p;j<=r-1;j++)
{
if(a[j]<=x)
{
i++;
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
temp=a[r];
a[r]=a[i+1];
a[i+1]=temp;
return i+1;
}

void quicksort2(int a[],int p,int r)
{
if(p<r)
{
int q=part2(a,p,r);
quicksort2(a,p,q-1);
quicksort2(a,q+1,r);
}
}

void quicksort1(string a[],int p,int r)
{
if(p<r)
{
int q=part1(a,p,r);
quicksort1(a,p,q-1);
quicksort1(a,q+1,r);
}
}
void output()
{
for(int i=0,j=0,k=0;i<len;i++)
{
if(i!=len-1)
{
if(flag[i]==1)//字符串
{
cout<<cha[j]<<", ";
j++;
}
else
{
cout<<dig[k]<<", ";
k++;
}
}
else
{
if(flag[i]==1)//字符串
{
cout<<cha[j]<<'.';
j++;
}
else
{
cout<<dig[k]<<'.';
k++;
}
}
}
cout<<'/n';
}

int main(void)
{
while(1)
{
if(input())
break;
partition();//将单词和数字分开
quicksort1(cha,0,clen-1);
quicksort2(dig,0,dlen-1);
output();
}
return 0;
}
[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: