UVa 1593 - Alignment of Code

题目描述 : 输入若干行代码,按照要求格式输出,。各列单词尽量靠左,单词之间至少要一个空格。

思路 : 利用字符串数组找规律。  只要控制好边界其他的都很简单。  连测试用例都没有用,因为UVa网页老刷不出来。直接交代码QuickSubmit,只是担心会超时,但结果令人意外,竟然是AC。再来两道吧。      对了 我又不好意思的用了正则表达式。

代码 :

<p>import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;</p><p>public class Main1593 {</p><p> public static void main(String[] args) {
  Scanner scan = new Scanner(System.in);
  Pattern p = Pattern.compile("<a target=_blank href="file://\\S">\\S</a>+");
  String[][] str = new String[1005][1000];
  int rows = 0;
  int[] rowcnt = new int[1000];
  Arrays.fill(rowcnt, 0);
  while(scan.hasNextLine()) {
   String line = scan.nextLine();
   Matcher m = p.matcher(line);
   int cols = 0;
   while(m.find()) {
    rowcnt[rows] ++;
    str[rows][cols++] = m.group();
   }
   rows ++;
  }
  //System.out.println(rows);
  int[] maxlen = new int[850];
  Arrays.fill(maxlen, 0);
  for(int i=0; i<rows; i++) {
   for(int j=0; j<rowcnt[i]; j++) {
    maxlen[j] = max(maxlen[j], str[i][j].length());
   }
  }
  for(int i=0; i<rows; i++) {
   for(int j=0; j<rowcnt[i]; j++) {
    System.out.print(str[i][j]);
    if(j < rowcnt[i]-1)
    for(int k=0; k<=maxlen[j]-str[i][j].length(); k++)
     System.out.print(" ");
   }
   System.out.println();
  }
 }
 public static int max(int a, int b) {
  if(a >= b)
   return a;
  else
   return b;
 }
}
</p>
时间: 2024-12-20 18:34:16

UVa 1593 - Alignment of Code的相关文章

UVa 1593 Alignment of Code(字符串)

题意  按要求对齐代码 字符串流的应用 #include <bits/stdc++.h> using namespace std; const int N = 1005, M = 200; string s[N][M], line; int cw[M], cn[N]; int main() { int r = 0, c = 0; while(getline(cin, line)) { stringstream ss(line); while(ss >> s[r][c]) { if(

UVA 1593: Alignment of Code(模拟 Grade D)

题意: 格式化代码.每个单词对齐,至少隔开一个空格. 思路: 模拟.求出每个单词最大长度,然后按行输出. 代码: #include <cstdio> #include <cstdlib> #include <cstring> char words[1200][190][90]; int maxLen[190]; char tmp[200]; typedef char * pchar; int readStr(pchar &str, char *out) { in

UVA 1839 Alignment

还是最长上升子序列... 本题是求队列中任一士兵都能从左边或者右边看到队伍外: 即某一士兵左边为上升子序列,右边为下降子序列.求两个序列和,再用总数减去: 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 #define maxn 1005 6 using namespace std; 7 8 double d[maxn]; 9 in

UVa 1593 (水题 STL) Alignment of Code

话说STL的I/O流用的还真不多,就着这道题熟练一下. 用了两个新函数: cout << std::setw(width[j]);    这个是设置输出宽度的,但是默认是在右侧补充空格 所以就要用cout.setf(ios::left);来设置一下左对齐. 1 #include <iostream> 2 #include <cstdio> 3 #include <sstream> 4 #include <vector> 5 #include &l

UVa 1593代码对齐

原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4468 这道题目的话可以使用iomanip这个头文件 1 #include<iostream> 2 #include<string> 3 #include<sstream> 4 #include<algorithm> 5 #inclu

poj 3959 Alignment of Code

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 ot

UVA - 1593

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

A - Alignment of Code(推荐)

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

Alignment of Code 解题心得

原题: ou 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 tabul