有些ACM题需要使用一些基本的数据结构,下面首先介绍与排序相关的内容。
1、基本排序
Problem Description
These days, I am thinking about a question, how can I get a problem as easy as A+B? It is fairly difficulty to do such a thing. Of course, I got it after many waking nights.
Give you some integers, your task is to sort these number ascending (升序).
You should know how easy the problem is now!
Good luck!
Input
Input contains multiple test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow. Each test case contains an integer N (1<=N<=1000 the number of integers to be sorted) and then N integers follow in the same line.
It is guarantied that all integers are in the range of 32-int.
Output
For each case, print the sorting result, and one line one case.
Sample Input
2
3 2 1 3
9 1 4 7 2 5 8 3 6 9
Sample Output
1 2 3
1 2 3 4 5 6 7 8 9
翻译:对给定的数字序列进行排序。使用数据结构的基本排序算法即可。可以使用各种排序算法实现。另外在Java中提供了排序方法,可以使用Arrays.sort方法,参考下面的代码:
Arrays.sort(a);
其中a是需要排序的数组。
下面给出了冒泡排序的代码供参考:
/*
* 冒泡排序
*/
public static int[] sort(int[] a){
// Arrays.sort(a);
for(int i=0;i<a.length-1;i++){
for(int j=a.length-1;j>i;j--){
if(a[j]<a[j-1]){
int temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
}
}
}
return a;
}
2、DNA Sorting
Description
One measure of ``unsortedness'' in a sequence is the number of pairs of entries that are out of order with respect to each other. For instance, in the letter sequence ``DAABEC'', this measure is 5, since D is greater than four letters to its right and E is greater than one letter to its right. This measure is called the number of inversions in the sequence. The sequence ``AACEDGG'' has only one inversion (E and D)---it is nearly sorted---while the sequence ``ZWQM'' has 6 inversions (it is as unsorted as can be---exactly the reverse of sorted).
You are responsible for cataloguing a sequence of DNA strings (sequences containing only the four letters A, C, G, and T). However, you want to catalog them, not in alphabetical order, but rather in order of ``sortedness'', from ``most sorted'' to ``least sorted''. All the strings are of the same length.
Input
The first line contains two integers: a positive integer n (0 < n <= 50) giving the length of the strings; and a positive integer m (0 < m <= 100) giving the number of strings. These are followed by m lines, each containing a string of length n.
Output
Output the list of input strings, arranged from ``most sorted'' to ``least sorted''. Since two strings can be equally sorted, then output them according to the orginal order.
Sample Input
10 6
AACATGAAGG
TTTTGGCCAA
TTTGGCCAAA
GATCAGATTT
CCCGGGGGGA
ATCGATGCAT
Sample Output
CCCGGGGGGA
AACATGAAGG
GATCAGATTT
ATCGATGCAT
TTTTGGCCAA
TTTGGCCAAA
翻译:对于一个序列的无序性可以使用相互之间无序的元素组的个数表示,例如字母序列DAABEC的无序性是5,D大于后面的AABC,E大于后面的C。而AACEDGG的无序性是1,因为只有E和D之间的顺序是乱的。
题目要求:对于给定的多个字符串(长度相同,由A, C, G和T组成),从最有序到最无序进行排列。
解题思路:计算每个字符串的无序性,然后进行排序即可。参考下面的代码:
/*
* DNA sorting
*/
public static void test3(String[] dnas){
Map invertions = new HashMap();
// 计算每个字符串的乱序数量
for(int i=0;i<dnas.length;i++){
invertions.put(dnas[i], count(dnas[i]));
}
// 排序
for(int i=0;i<dnas.length-1;i++){
for(int j=dnas.length-1;j>i;j--){
if((Integer)(invertions.get(dnas[j]))<(Integer)(invertions.get(dnas[j-1]))){
String temp = dnas[j];
dnas[j] = dnas[j-1];
dnas[j-1] = temp;
}
}
}
// 输出
for(String temp:dnas){
System.out.println(temp+invertions.get(temp));
}
}
// 计算无序性
public static int count(String dna){
int sum=0;
for(int i=0;i<dna.length()-1;i++){
char temp = dna.charAt(i);
for(int j=i+1;j<dna.length();j++){
if(temp>dna.charAt(j))
sum++;
}
}
return sum;
}
上面计算无序性的代码是通过遍历字符串中的每个元素,然后判断后续的每个字符和它之间是否有序,需要遍历两次。如果字符串不是很长,这种算法还可以接受。如果字符串很长,计算将很耗时。下面的方法进行了改进。
/*
* 计算无序性(改进算法)
*/
publicstaticint count2(String dna){
int count[] = newint[4];
Arrays.fill(count,0);
int sum=0;
for(int i=dna.length()-1;i>=0;i--){
char temp = dna.charAt(i);
switch(temp){
case'A':
count[0]++;
break;
case'C':
count[1]++;
sum = sum+count[0]; // 加上C后面的A的个数
break;
case'G':
count[2]++;
sum = sum+count[0]+count[1]; // 加上G后面的A和C的数量
break;
case'T':
count[3]++;
sum = sum+count[0]+count[1]+count[2]; // 加上G后面的A、C和G的数量
break;
}
}
return sum;
}
另外在代码中,对序列进行排序是自己编写的排序算法。也可以使用系统提供的排序功能,需要编写一个类,参考代码如下:
class DNA implements Comparable{
String content;
int unsortness;
DNA(String content,int unsortness){
this.content = content;
this.unsortness = unsortness;
}
@Override
public int compareTo(Object o) {
return unsortness-((DNA)o).unsortness;
}
}
修改后的代码如下:
/*
* DNA sorting
*/
publicstaticvoid test3(String[] dnas){
DNA newDnas[] = new DNA[dnas.length];
for(int i=0;i<dnas.length;i++){
newDnas[i] = new DNA(dnas[i],count(dnas[i]));
}
Arrays.sort(newDnas); // 替换了原来的排序算法
// 输出
for(DNA temp:newDnas){
System.out.println(temp.content+temp.unsortness);
}
}
分享到:
相关推荐
ACM准备模板 堆排序模板 acm 堆 排序
ACM 模版ACM 模版ACM 模版ACM 模版
本次课程论文,针对ACM比赛中的经典算法,动态规划,进行了详细的讲述,并以ZOJ和POJ上的经典题目为例,讲述了动态规划算法的应用。
北大acm.pku——40道简单题源码 全部本人原创,虽然写的一般但是一点心意,和大家分享, 都是水题,希望对大家有所帮助。
NULL 博文链接:https://yuyongjia.iteye.com/blog/1626488
ACM算法 题目解析 PPT教学 还有详细的题目讲解
ACM作业平台1-57题题目+AC代码,浙江工商大学信电学院C语言期末上机题目,代码入门级的C语言设计题目,适合初学C语言的同学,内容覆盖全面。
非常实用,非常经典!!!!!绝对独家!!!内部资料!!!
这是我在平时在网上收集的。 资源里面大概是些ACM历史,初赛,等一些试题。
一 个报告,新手用 没什么用的 随便看看个报告,新手用 没什么用的 随便看看个报告,新手用 没什么用的 随便看看
acm图论.ppt————电子版_ppt版
一高手的训练计划,分阶段介绍了训练的方向和内容,有用与否?你懂的~!!!
ACM数论——ppt(天津大学)ACM数论——ppt(天津大学)
ACM培训——算法入门---------------------------------算法入门ACM培训——算法入门---------------------------------算法入门ACM培训——算法入门---------------------------------算法入门
模板ACM———浙江大学与吉林大学 模板ACM———浙江大学与吉林大学 模板ACM———浙江大学与吉林大学 模板ACM———浙江大学与吉林大学 模板ACM———浙江大学与吉林大学
给刚接触 ACM 的同学提供做题时输入输出的知识。
大学生acm竞赛——10年12月竞赛题库 希望对你们有所帮助
清华大学ACM模板,汇集了很多算法的标准模式,而且有详细的算法讲解,图论、数论、网络流等等,从浅到深,彻底解剖,是ACM进阶者不二的选择~!
给刚接触ACM的同学提供字符串处理和有关随机数生成知识。
练习场 - ACM在线评测系统的第八题,用c++写的