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 temp[300];
			memset(temp,'\0',sizeof(temp));
			for(int i=hb;i<hb+ha;i++){
				temp[i]=a[i-hb];
			}
			for(int i=0;i<ha+hb;i++){
				bool ok=false;
				for(int j=i;j<hb+i;j++){
					if(temp[j]+b[j-i]>'1'+'2'){
						ok=true;
						break;
					}
				}
				if(!ok){
					if(i<hb) ans=ha+hb-i;
		      else if(i>=hb&&i<ha) ans=ha;
					else if(i<ans) ans=i;
				}
			}
			return ans;

  }

int main(void){
	  char a[105],b[105];
	  while(scanf("%s %s",a,b)==2){
	  	int ha=strlen(a),hb=strlen(b);
	  	int ans;
	  	if(ha>=hb) ans=make(a,b,ha,hb);
	  	else ans=make(b,a,hb,ha);
	  	printf("%d\n",ans);
	  }
		return 0;
}

时间: 2024-07-29 11:22:04

UVa 1588 字符串的相关文章

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 232 字符串处理、

背景:做了三个半小时,代码能力堪忧啊,各种调试,各种出错,要分析一下,这些错点尽量不能再错. 学习:1.对于字符串数组,要把每一行都开大一位,该位用来存放'\0',否则将会出现未知输出.也就是说:字符串二维数组的每一行都可以看做一个字符数组,结尾都有一个'\0'.printf在用'%s'格式符输出字符串,总是从给定的首地址开始,遇到'\0'结束. 2.写程序的时候要有动态的眼光来看待当前写下的代码运行时的样子.运行出错不要理解单步调试,因先猜测是哪里错了,先看代码在脑中模拟. #include<

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 1588 - Kickdown

从头到尾扫两边即可 1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 char a[105],b[105]; 5 int aa[105],bb[105]; 6 int la,lb,ans; 7 bool flag; 8 void fuc(int a[],int b[],int la,int lb) 9 { 10 int tmp=0; 11 int i,j,k; 12 i=j=k=0; 13 whi

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 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 输出: -- -- -- | | | | | | | | | | | | -- -- -- -- | | | | | | | | | | -- -- -- --- --- --- --- --- | | | | | | | | | | | | | | | | | | | | | | | | --- --- --- | | | | | | | |

poj 3158 Kickdown 字符串匹配?

也是uva 1588 这种题目不禁让人想到KMP 可是这个长度才100啊想怎么暴力就怎么暴力 用的就是传说中的BF算法(brute-force) 把短串加到长串的两边 然后匹 swap和strncpy用的飞起不枉我重修了一遍计导和c语言 poj题目链接:http://poj.org/problem?id=3158 #include <cstdio> #include <cstring> #include <cstdlib> #include <iostream&g