字符/字符串 操作

Code Formatter
Time limit: 1 Seconds   Memory limit: 32768K  
Total Submit: 1277   Accepted Submit: 364

  Some companies have special requirements for source code format, and it is also good for programmers to keep consistent code style. You are asked to write a simple code formatter for the company to help the poor programmers. 
The first thing you need to do is to check whether the source code contains tabs (represented as the escape character '\t'), since different terminals have different ways to display tabs, it's better not to use them, but replace them with spaces. The code formatter should replace each tab of the source code with 4(four) blank spaces. 
Then you need to remove trailing spaces of the source file. Trailing spaces are one or more consecutive whitespaces right before the EOL (end of line, represented as the escape character '\n'), and they usually have no meaning in most programming language, so they can be safely removed. 
Input 
The input contains multiple test cases! 
The first line is an integer N indicating the number of test cases. Each test case is the source which contains not more than 100 lines given to you to format. A single line containing only "##" marks the end of a test case. 
Output 
For each test case, output a log of the formatter in two lines of the following format: 
#A tab(s) replaced #B trailing space(s) removed Where #A is the number of tabs replaced and #B is the number of trailing spaces removed. 
Sample Input 

include <stdio.h> 

int main() 
{ 
(tab一个制表符)int a,b; 
(tab一个制表符)while(scanf("%d %d",&a, &b) != EOF) 
(tab一个制表符)(tab一个制表符)printf("%d\n",a+b);               

} 
## 

## 
Sample Output 
4 tab(s) replaced 
22 trailing space(s) removed 
0 tab(s) replaced 
0 trailing space(s) removed 
Note 
In order to show the whitespaces precisely, all the characters in sample input are underlined. They are not the underscore character. 
Author: 
YANG, Chao 

-------------------------------------------------------------------------------------------------- 

URL:http://acm.zju.edu.cn/show_problem.php?pid=2851 


这道题目一开始我走了弯路,按字符读入,并判断是否为空格和TAB,并对每次连在一起的空格和TAB进行记数,如果下一个字符不是回车或TAB或空格,那么计数器清零,由于要判断结束标志##,正确的说是被两个回车(\n)包围起来的##,所以要用4个字符变量进行存储读入的字符...事实证明,这样会超时----时间复杂度太大,每个字符读入都要让计算机运行4次判断操作 



后来按行读入字符串,对字符串进行操作,有效地将时间复杂度转变成空间复杂度. 
1.将输入的字符串中的TAB全部替换成空格(注意)并返回TAB的数量 
2.用space函数,计算行末空格的数量 
  用一个变量P存储字符串的长度,然后从后面往前判断,一但字符是空格,退出并返回该行的行末空格数 
注意:替换空格的时候,要考虑到\0,如果不考虑到\0,那么*(s+p+3)=*(s+p)这条循环语句将覆盖掉\0,会出错 

代码: 

#include <stdio.h> 
#include <string.h> 
int tab(char s[]) 
{ int i,t,q; 
  i=0; 
  t=0; 
  while(*(s+i)!='\0') 
  { if(*(s+i)=='\t') 
    { t++; 
      for(q=strlen(s);q>i;q--) 
        *(s+q+3)=*(s+q); 
      *(s+i)=' '; i++; 
      *(s+i)=' '; i++; 
      *(s+i)=' '; i++; 
      *(s+i)=' '; 
    } 
    i++; 
  } 
  return(t); 
} 
int space(char s[]) 
{ int i,k; 
  i=strlen(s)-1; 
  k=0; 
  while(*(s+i)==' ') 
  { k++; 
    i--; 
  } 
  return(k); 
} 
main() 
{ char sa[10000]; 
  int ta,i,t,s; 
  scanf("%d",&ta); 
  getchar(); 
  for(i=0;i<ta;i++) 
  { t=0; 
    s=0; 
    while(1) 
    { gets(sa); 
      if(strcmp(sa,"##")==0) 
        break; 
      t+=tab(sa); 
      s+=space(sa); 
    } 
    printf("%d tab(s) replaced\n%d trailing space(s) removed\n",t,s); 
  } 
}

 

posted @ 2012-05-10 17:05  陈小怪  阅读(196)  评论(0编辑  收藏  举报