UVa 232 字符串处理、

背景:做了三个半小时,代码能力堪忧啊,各种调试,各种出错,要分析一下,这些错点尽量不能再错。

学习:1.对于字符串数组,要把每一行都开大一位,该位用来存放‘\0‘,否则将会出现未知输出。也就是说:字符串二维数组的每一行都可以看做一个字符数组,结尾都有一个‘\0‘.printf在用‘%s‘格式符输出字符串,总是从给定的首地址开始,遇到‘\0‘结束。

2.写程序的时候要有动态的眼光来看待当前写下的代码运行时的样子。运行出错不要理解单步调试,因先猜测是哪里错了,先看代码在脑中模拟。

#include<stdio.h>
#include<string.h>
int main(void)
{
	int n, m, count = 1;
	while (scanf("%d", &n) == 1 && n){
		scanf("%d",&m);
		char puzzle[n][m];
		for (int i = 0; i<n; i++) scanf("%s", puzzle[i]);
		int num[n][m];
		memset(num, 0, sizeof(num));
		int count1 = 1;
		for (int i = 0; i<n; i++)
		for (int j = 0; j<m; j++){
			if ((i - 1<0 || j - 1<0 || puzzle[i - 1][j] == '*' || puzzle[i][j - 1] == '*')&&puzzle[i][j]!='*') {
				num[i][j] = count1++;
			}
			else{
				num[i][j] = 0;
			}
		}
		if (count - 1) printf("\n");
		printf("puzzle #%d:\n", count++);

        //Across deal.
		printf("Across\n");
		for (int i = 0; i < n; i++)
		for (int j = 0, key = 1; j < m; j++){
			if (num[i][j]){
				while (1){
					if (j + key < m&&puzzle[i][j + key] != '*') key++;
					else break;
				}
				printf("%3d.", num[i][j]);
				for (int ii=j; j < ii + key; j++) printf("%c", puzzle[i][j]);
				printf("\n");
			}
                        key=1;
		}

		//Down deal.
		char ans[count1][n+1];
		memset(ans,'\0',sizeof(ans));
		printf("Down\n");
		for (int i = 0; i < m; i++)
		for (int j = 0, key = 1; j < n; j++){
			if (num[j][i]){
				while (1){
					if (j + key < n&&puzzle[j+key][i] != '*') key++;
					else break;
				}
				/*printf("%2d.", num[j][i]);
				for (int ii=j; j < ii + key; j++) printf("%c", puzzle[j][i]);
				printf("\n");*/
				for (int ii=j,kk=0,gg=num[j][i]; j < ii + key; j++,kk++) {
				ans[gg][kk]=puzzle[j][i];
		    	}
			}
                        key=1;
		}
		for(int jj=1;jj<count1;jj++){
			if(ans[jj][0]!='\0'){
			   printf("%3d.", jj);
			   printf("%s\n",ans[jj]);
			}
		}
	}
	return 0;
}
时间: 2024-08-29 07:37:22

UVa 232 字符串处理、的相关文章

UVa 10340 字符串基础

背景:小紫书习题,开始数组开小了runtime error了一次,显然数组越界.复杂度:O(max(A的长度,B的长度)). 题意:看字符串A是不是字符串B的子串.直接顺序扫描即可. #include<stdio.h> #include<string.h> char str[1000000],ttr[1000000]; int main(void){ while(scanf("%s %s",str,ttr)!=EOF){ int j=0,sj=strlen(st

UVa 12206 (字符串哈希) Stammering Aliens

体验了一把字符串Hash的做法,感觉Hash这种人品算法好神奇. 也许这道题的正解是后缀数组,但Hash做法的优势就是编码复杂度大大降低. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 6 const int maxn = 40000 + 10; 7 const int x = 123; 8 int n, m, pos; 9 unsigne

UVA 1585 字符串处理

背景:小紫书上习题 学习:1.条件运算符?:: 的运用可以简化,高效代码.?的优先级大于=,小余算术和关系运算符.与多重赋值语句一样采用右结合.(用到了dp的思想) 代码: #include<stdio.h> #include<string.h> int main(void){ int num[80]; char str[81]; int t; scanf("%d",&t); while(t--){ int sum = 0; scanf("%s

UVa 232 - Crossword Answers

横排纵排找单词 先标记,再记录答案最后排序,排序时一定要对准首末位置 1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 #include <iomanip> 5 using namespace std; 6 struct T{ 7 int num; 8 char ans[15]; 9 }down[300],acr[300]; 10 int r,c,cnt1,cnt2,cnt

UVA 1586 字符串处理

背景:做了快40分钟,还是好多细节是调试过来的,看来距离150行以内代码一次通过的能力还很远. 学习:1.变量定义的时候不仅要想到初始化,更要想到初始化的位置,这个变量的作用域如果开大了,和没初始化一样的效果. 代码: #include<stdio.h> #include<string.h> int main(void){ char str[85]; int t; scanf("%d",&t); while(t--){ memset(str,'S',si

UVa 1588 字符串

背景:1--WA:很难想到还有如下情况(见图),认真读题后发现!!! 思路:就是吧两个部件所有重合情况都考虑了,取其中满足的最小长度. 学习:1.'1'+'2'='3'是错误的!!!!!!!!! #include<stdio.h> #include<string.h> int make(char a[],char b[],int ha, int hb); int make(char a[],char b[],int ha,int hb){ int ans=ha+hb; char t

UVA 1225 字符串处理

背景:无. #include<stdio.h> #include<string.h> int main(void){ int t,str[10]; scanf("%d",&t); while(t--){ int n; memset(str,0,sizeof(str)); scanf("%d",&n); for(int i=1;i<=n;i++){ if(i/1000) {str[i/1000]++;str[(i/100)

UVA 706 LCD Display 液晶显示屏 (字符串模拟)

[题目链接]click here~~ [题目大意] 给定的数字序列,按照要求输出对应液晶显示屏上的数字 输入: 2 12345 3 67890 0 0 输出: -- -- -- | | | | | | | | | | | | -- -- -- -- | | | | | | | | | | -- -- -- --- --- --- --- --- | | | | | | | | | | | | | | | | | | | | | | | | --- --- --- | | | | | | | |

UVa 401 Palindromes(字符串,回文)

 Palindromes  A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDEDCBA" is a palindrome because it is the same when the string is read from left to right as when the string i