代码对齐(Alignment of Code)

2023-05-16

Alignment of Code

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 958    Accepted Submission(s): 230

Problem Description
You are working in a team that writes Incredibly Customizable Programming Codewriter (ICPC) which is basically a text editor with bells and whistles. You are working on a module that takes a piece of code containing some definitions or other tabular information and aligns each column on a fixed vertical position, while keeping the resulting code as short as possible, making sure that only whitespaces that are absolutely required stay in the code. So, that the first words on each line are printed at position p 1 = 1; the second words on each line are printed at the minimal possible position p 2, such that all first words end at or before position p 2 - 2; the third words on each line are printed at the minimal possible position p 3, such that all second words end at or before position p 3 - 2, etc.
For the purpose of this problem, the code consists of multiple lines. Each line consists of one or more words separated by spaces. Each word can contain uppercase and lowercase Latin letters, all ASCII punctuation marks, separators, and other non-whitespace ASCII characters (ASCII codes 33 to 126 inclusive). Whitespace consists of space characters (ASCII code 32).
 

Input
The input begins with an integer T. The next T blocks each represents a case. Each case contains one or more lines of the code up to the end of case mark-a single '@' in a single line. All lines are terminated by a standard end-of-line sequence in the file. Each line contains at least one word, each word is 1 to 80 characters long (inclusive). Words are separated by one or more spaces. Lines of the code can have both leading and trailing spaces. Each line in the input file is at most 180 characters long. There are at most 1000 lines for each case.
 

Output
For each case, write to the output file the reformatted, aligned code that consists of the same number of lines, with the same words in the same order, without trailing and leading spaces, separated by one or more spaces such that i-th word on each line starts at the same position p i.
 

Sample Input

  
  
1 start: integer; // begins here stop: integer; // ends here s: string; c: char; // temp @
 

Sample Output

  
  
start: integer; // begins here stop: integer; // ends here s: string; c: char; // temp

【分析】

       以空格为界将每行中的每个“词”储存起来,由于每行的“词”不定,所以用链表进行储存(当然也可以用数组,只要数组的大小足够大),那么每一行形成一个链表,就有多个链表,构成一个数组。在读取“词”的过程中,记录每一列中“词”最大的长度。记录在一个链表中(这个链表的每个元素是输入文本的每一列词最大长度的值)。

/*

java中字符串的split方法的参数含义:
public String[] split(String regex)根据给定的正则表达式的匹配来拆分此字符串.

split("\\s+") 中

\\s表示 空格,回车,换行等空白符,
+号表示一个或多个的意思

*/

用java语言编写程序,代码如下:

import java.util.ArrayList;
import java.util.Scanner;
//终于对了~~~!!注意格式问题!!
public class Main {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);		
		int t = input.nextInt();
		input.nextLine();//注意读取回车符。这是我第二次吃大亏了!!!
		
		for(int ti = 0; ti < t; ti++) {
			ArrayList<String>[] list = new ArrayList[1001];
			ArrayList<Integer> colsMaxLen = new ArrayList<Integer>();
			for(int i = 0; i < 1001; i++) {
				list[i] = new ArrayList<String>();
				colsMaxLen.add(0);
			}
			
			String s;
			String[] tempArr;
			int arrLen = 0;
			int line = 0;
			while(true) {
				s = input.nextLine();
				if(s.equals("@"))
					break;
				
				tempArr = s.split("\\s+");
				//上述正则表达式中  \\s表示 空格,回车,换行等空白符,+号表示一个或多个的意思,
				
				arrLen = tempArr.length;
				int first = 0;
				//为什么要有下面的语句的呢?理由是用split切割时,字符串前面有空格的话,似乎会多切出一个空字符,那么这个空字符就应该省略
				if(tempArr[0].equals(""))
					first = 1;
				
				for(int pos = 0; first < arrLen; first++, pos++) {
					list[line].add(tempArr[first]);
					if(colsMaxLen.get(pos) < tempArr[first].length())
						colsMaxLen.set(pos, tempArr[first].length());
				}
				
				line++;
			}
			
			print(line, list, colsMaxLen);
		}
		
	}
	
	public static void print(int line, ArrayList<String>[] list, ArrayList<Integer> colsMaxLen) {
			for(int i = 0; i < line; i++) {
				for(int j = 0; j < list[i].size() - 1;j++) {
					//printWord(list[i].get(j), colsMaxLen.get(j), j);
					if(j != 0)
						System.out.print(" ");
					System.out.printf("%-" + colsMaxLen.get(j) + "s", list[i].get(j));
				}
				//注意最后每一行最后一个“词”不用进行格式化,而是直接输出回车就行。
				if(list[i].size() != 1)
					System.out.print(" ");
				System.out.println(list[i].get(list[i].size() - 1));
			}
	}
	
	/*public static void printWord(String s, int len, int cols) {
		if(cols != 0)
			System.out.print(" ");
		System.out.print(s);
		for(int i = 0; i < len - s.length(); i++)
			System.out.print(" ");
	}*/
}


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

代码对齐(Alignment of Code) 的相关文章

  • 如何在 Android 中将 ImageButton 与背景图像正确对齐?

    在使用 XML 的 Android UI 设计中 如何将 ImageButton 与 Activity xml 文件的背景完全对齐 假设我有两张图像 一张用作活动的背景图像 第二张用作图像按钮源 这是背景图像 https i stack i
  • 为什么我的组件超出了边界,请帮助我将其与必要的代码对齐

    import net java dev designgridlayout Tag import net java dev designgridlayout DesignGridLayout import javax swing table
  • 两个 div 中的段落对齐方式,中间有图像

    我怎样才能对齐我的段落 如下图所示 我需要展示一份报纸之类的东西 其中应该包含此内容 以下是我正在使用的html代码 div class left div div class right div div class myImage img
  • 垂直对齐无序列表导航链接?

    我发现这个问题已经被问过很多次了 但我认为我的情况略有不同 我正在处理的网站的导航是用无序列表构建的 如下所示 div class nav root nav area top ul class nav root wrap li class
  • 突变残基和位置的数字编码

    我正在编写一个 python 程序 它必须计算突变残基和位置的数字编码一组字符串 这些字符串是蛋白质序列 这些序列存储在 fasta 格式文件中 每个蛋白质序列用逗号分隔 不同蛋白质的序列长度可能不同 在此我试图找到以下位置和序列 变异了
  • DataGridView 单元格对齐不起作用

    我有一个在 C Winforms 项目中使用的 DataGridView 网格不会自动生成列 我已手动设置列名称和属性 这包括单元格的 DefaultCellStyle 我希望 DataGridViewCheckBoxColumn 类型的所
  • 将 Z 轴与向量对齐的最简单方法是什么?

    给定一个点 如 0 0 0 和一个向量 如 x y z 对齐以 0 0 0 为中心的负 Z 轴以指向该向量的方向的最简单方法是什么 使用 OpenGL 的示例将受到欢迎 但不是必需的 有很多不同的方法可以旋转坐标系以指向给定方向 它们都会使
  • iTextsharp 中的多表列对齐

    我正在创建一个表 其中每列都有自己的对齐方式 如下所示 如何在列级别而不是单元格级别完成它 iText 和 iTextSharp 不支持列样式和格式 做到这一点的唯一方法就是像您当前正在做的那样 逐个单元地进行 EDIT 最简单的解决方法是
  • gcc 如何在特定平台上获得每种类型的对齐方式?

    它是硬编码到 gcc 的源代码中还是以编程方式获取 我认为它是硬编码在特定于 arch 的文件夹中的 例如对于sparc http www google com codesearch Yj7Hz1ZInUg trunk gcc 4 2 1
  • 单选按钮的文本换行不正确

    I have 2 html radio buttons separated by br tags where the text is wrapping under the radio button instead of aligning w
  • 使用UIScrollView,缩小时如何使内容视图保持在中心?

    最小比例为 0 5 最大比例为2 0 我需要总是使用更大的contentSize for UIScrollView能够拖动内容 所以 作为CGRect内容正在发生变化 contentSize of UIScrollView也在改变 然而 当
  • 左、中、右对齐同一行底部的 div

    我想在同一行显示三个 div 这三个都有不同的宽度和高度 并且它们不是直文本 我想左对齐一个 一直到左边 右对齐另一个 一直到右边 然后将第三个居中 在包含 div 的中间 在本例中是整个页面 此外 我希望这三个 div 与包含的 div
  • 如何强制内联块 div 达到相同高度

    我有像网格一样的内联块 div 我想强迫所有在同一行的人达到相同的高度 他们应该得到最长的div的高度 CSS jquery 或简单的 javascript 解决方案会很棒 现在这很常见 我去看了Masonry 但据我对示例图形的理解 它并
  • UITableView 的文本右对齐且缩进

    我想做一个UITableView文本右对齐且缩进 如下图所示 不幸的是 我不能通过写来做到这一点 cell textLabel textAlignment UITextAlignmentRight cell indentationLevel
  • 如何进行C++对齐数组分配?

    我想修改数组分配 float a new float n 使用对齐的分配器 我倾向于尝试使用placement new 和 posix memalign 或新的 c 11 等效项 但请注意数组的新放置对于数组分配来说是有问题的 https
  • 有关对齐的问题

    在函数内的局部作用域中声明时将所有相同类型的变量分组在一起是一个好习惯吗 如果是 为什么 它能解决内存对齐问题吗 我认为这对我 20 年前使用的 VAX C 编译器很重要 但对任何现代编译器都没有影响 这是not可以安全地假设局部变量将按任
  • 用户代码可以安全地使用结构体填充吗?

    假设我有一个如下所示的结构 struct Struct char Char int Int and sizeof int 大于 1 编译器会添加填充Char成员变量 编译器生成的代码是否允许更改填充字节的值 我的意思是 如果我使用指针算术并
  • Swift UITableViewCell 对齐

    我正在尝试显示如下页面 页面标题 左对齐 区域名称 中心对齐 该地区的人 详细标签中带有电子邮件的副标题 但是 如果我选择副标题作为单元格样式 则所有内容都将左对齐 并且我无法在代码中更改它们 然后 如果我选择自定义作为样式 则详细文本标签
  • 如何在标签下方水平对齐单选按钮

    因此 我使用以下 HTML 在 jsp 中的各自标签下方水平居中显示 4 个单选按钮
  • 卡住。如何将这 3 个文本居中并间隔开? [复制]

    这个问题在这里已经有答案了 尝试向左 居中 向右移动 3 段文本 尝试使用 CSS 但我陷入困境 Thanks 您可以使用 flex 并将 justify content 设置为 space Between text container d

随机推荐

  • 排列(permutation)

    排列 xff08 permutation xff09 用1 2 3 xff0c xff0c 9组成3个三位数 abc xff0c def 和 ghi xff0c 每个数字恰好使用一次 xff0c 要求 abc def ghi 61 1 2
  • SQL Server Management Studio 使用方法手记

    本方法只记录自己的操作方法 xff0c 仅供参考 一 下载安装 SQL Server Management Studio V15 0 18424 0 xff0c 链接 xff1a https download csdn net downlo
  • 蛇形填数(矩阵)

    蛇形填数 在 n x n 方针里填入 1 2 xff0c xff0c n x n xff0c 要求填成蛇形 例如 xff1a n 61 4时方阵为 xff1a 10 11 12 1 9 16 13 2 8 15 14 3 7 6 5 4 上
  • WERTYU

    Problem 1343 WERTYU Time Limit 1000 mSec Memory Limit 32768 KB Problem Description A common typing error is to place the
  • 分子量(Molar Mass)

    Molar mass Time limit 3 000 seconds An organic compound is any member of a large class of chemical compounds whose molec
  • 数数字(Digit Counting)

    Digit Counting Time limit 3 000 seconds Trung is bored with his mathematics homeworks He takes a piece of chalk and star
  • 周期串(Periodic Strings)

    周期串 Periodic Strings 如果一个字符串可以由某个长度为k的字符串重复多次得到 xff0c 则称该串以k为周期 例如 xff0c abcabcabcabc以3为周期 xff08 注意 xff0c 它也以6和12为周期 xff
  • 谜题(Puzzle)

    Puzzle Time limit 3 000 seconds Puzzle A children 39 s puzzle that was popular 30 years ago consisted of a 5x5 framewhic
  • 纵横字谜的答案(Crossword Answers)

    Crossword Answers Time limit 3 000 seconds Crossword Answers A crossword puzzle consists of a rectangular grid of black
  • DNA序列(DNA Consensus String)

    DNA Consensus String Time limit 3 000 seconds Figure 1 DNA Deoxyribonucleic Acid is the molecule which contains the gene
  • 古老的密码(Ancient Cipher)

    Ancient Cipher Time limit 3 000 seconds Ancient Roman empire had a strong governmentsystem with various departments incl
  • Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing

    原文转载自 sunny2038 的CSDN博客文章 原文地址 最近在看Java xff0c 在编译写书上一个例子时 xff0c 由于书上的代码只有一部分 xff0c 于是就自己补了一个内部类 结果编译时出现 xff1a No enclosi
  • 瑞星微开发工具下载镜像的配置方法?

    如何根据 parameter txt 建立配置表 xff1f 首先我们来看下 parameter txt 的内容 xff1a CMDLINE mtdparts 61 rk29xxnand 0x00002000 64 0x00004000 u
  • 特别困的学生(Extraordinarily Tired Students)

    Extraordinarily Tired Students Time limit 3 000 seconds When a student is too tired he can 39 t help sleeping in class e
  • 骰子涂色(Cube painting)

    Cube painting We have a machine for painting cubes It is supplied with three different colors blue red and green Each fa
  • 盒子(Box)

    Box Time limit 3 000 seconds Ivan works at a factory that produces heavy machinery He hasa simple job he knocks up woode
  • 循环小数(Repeating Decimals)

    Repeating Decimals Time limit 3 000 seconds Repeating Decimals The decimal expansion of the fraction 1 33 is wherethe is
  • 反片语(Ananagrams)

    反片语 Ananagrams 输入一些单词 xff0c 找出所有满足如下条件的单词 xff1a 该单词不能通过字母重排 xff0c 得到输入文本中的另外一个单词 在判断是否满足条件时 xff0c 字母不分大小写 xff0c 但在输出时应保留
  • 集合栈计算机(The SetStack Computer)

    The SetStack Computer Time limit 3 000 seconds 题目是这样的 xff1a 有一个专门为了集合运算而设计的 集合栈 计算机 该机器有一个初始为空的栈 xff0c 并且支持以下操作 xff1a PU
  • 代码对齐(Alignment of Code)

    Alignment of Code Time Limit 4000 2000 MS Java Others Memory Limit 32768 32768 K Java Others Total Submission s 958 Acce