HDU 2203 strstr()函数应用

本文介绍了一种特殊的字符串匹配方法,通过扩展原始字符串并使用strstr函数来判断一个字符串是否为另一个字符串的子串。此方法适用于需要高效查找子串的场景。

http://acm.hdu.edu.cn/showproblem.php?pid=2203

/*

char* strstr(const char* a,constchar* b)  在a中寻找第一次出现b的位置

如果出现,返回位置,如果不出现,返回空指针;

利用这一特点,我们将s1字符串长度加长一部分(s2的长度),加长的内容为s1的前strlen(s2)个字符;

最后用函数判断s2是否在s1中出现;

over~~

*/


	#include <iostream>
	#include <cstdio>
	#include <cstring>
	#include <algorithm>
	#include <cmath>
	#include <string>
	#include <iomanip>
	#define maxn 100005
	using namespace std;
	char s1[2*maxn],s2[maxn];
	int main(int argc, char *argv[])
	{
		while(scanf("%s%s",&s1,&s2)!=EOF)
		{
			int len1 = strlen(s1),len2 = strlen(s2);
			if(len2>len1)
			{
				puts("no");
				continue;
			}
			for(int i = len1,j = 0; i < len1+len2; i++)
			{
				s1[i] = s1[j++];
			}
			
			len1 = len1+len2;
			
			if(strstr(s1,s2)!=NULL)
			{
				puts("yes");
			}
			else
			{
				puts("no");
			}
		}
		return 0;
	}


static void parse_gprmc(nmea_msg *gps, const char *buf) { const char *p = strstr(buf, "GPRMC"); if (!p) return; uint8_t pos; uint8_t dx; // UTC时间 pos = nmea_comma_pos((const uint8_t *)p, 1); if (pos != 0xFF) { uint32_t time = nmea_str2num((const uint8_t *)(p + pos), &dx); gps->utc.hour = time / 10000; gps->utc.min = (time % 10000) / 100; gps->utc.sec = time % 100; } // 状态检查 (A=有效, V=无效) pos = nmea_comma_pos((const uint8_t *)p, 2); if (pos != 0xFF && *(p + pos) == 'V') { gps->fixmode = 0; // 无效定位 return; } // 纬度 pos = nmea_comma_pos((const uint8_t *)p, 3); if (pos != 0xFF) { float lat_val = nmea_str2num((const uint8_t *)(p + pos), &dx) / 100.0f; int deg = (int)(lat_val) / 100; float min = lat_val - deg * 100; gps->latitude = deg + min / 60.0f; } // 纬度半球 pos = nmea_comma_pos((const uint8_t *)p, 4); if (pos != 0xFF) gps->nshemi = *(p + pos); // 经度 pos = nmea_comma_pos((const uint8_t *)p, 5); if (pos != 0xFF) { float lon_val = nmea_str2num((const uint8_t *)(p + pos), &dx) / 100.0f; int deg = (int)(lon_val) / 100; float min = lon_val - deg * 100; gps->longitude = deg + min / 60.0f; } // 经度半球 pos = nmea_comma_pos((const uint8_t *)p, 6); if (pos != 0xFF) gps->ewhemi = *(p + pos); } // 完整GPGGA解析 static void parse_gpgga(nmea_msg *gps, const char *buf) { const char *p = strstr(buf, "GPGGA"); if (!p) return; uint8_t pos; uint8_t dx; // UTC时间 pos = nmea_comma_pos((const uint8_t *)p, 1); if (pos != 0xFF) { uint32_t time = nmea_str2num((const uint8_t *)(p + pos), &dx); gps->utc.hour = time / 10000; gps->utc.min = (time % 10000) / 100; gps->utc.sec = time % 100; } // 纬度 pos = nmea_comma_pos((const uint8_t *)p, 2); if (pos != 0xFF) { float lat_val = nmea_str2num((const uint8_t *)(p + pos), &dx) / 100.0f; int deg = (int)(lat_val) / 100; float min = lat_val - deg * 100; gps->latitude = deg + min / 60.0f; } // 纬度半球 pos = nmea_comma_pos((const uint8_t *)p, 3); if (pos != 0xFF) gps->nshemi = *(p + pos); // 经度 pos = nmea_comma_pos((const uint8_t *)p, 4); if (pos != 0xFF) { float lon_val = nmea_str2num((const uint8_t *)(p + pos), &dx) / 100.0f; int deg = (int)(lon_val) / 100; float min = lon_val - deg * 100; gps->longitude = deg + min / 60.0f; } // 经度半球 pos = nmea_comma_pos((const uint8_t *)p, 5); if (pos != 0xFF) gps->ewhemi = *(p + pos); // 定位质量 pos = nmea_comma_pos((const uint8_t *)p, 6); if (pos != 0xFF) gps->fixmode = nmea_str2num((const uint8_t *)(p + pos), NULL); // 卫星数 pos = nmea_comma_pos((const uint8_t *)p, 7); if (pos != 0xFF) gps->posslnum = nmea_str2num((const uint8_t *)(p + pos), NULL); // 海拔高度 pos = nmea_comma_pos((const uint8_t *)p, 9); if (pos != 0xFF) gps->altitude = nmea_str2num((const uint8_t *)(p + pos), &dx) / 10.0f; } // 主解析函数 void gps_parse_nmea(nmea_msg *gps, const char *buf) { // 校验和验证 const char *end = strchr(buf, '*'); if (end) { uint8_t calc_check = nmea_str2num(buf); uint8_t recv_check = strtoul(end + 1, NULL, 16); if (calc_check != recv_check) { return; // 校验失败 } } if (strstr(buf, "GPRMC")) parse_gprmc(gps, buf); else if (strstr(buf, "GPGGA")) parse_gpgga(gps, buf); } // 显示数据 void gps_show_data(const nmea_msg *gps) { if (gps->fixmode == 0) { printf("No valid GPS fix\n"); return; } printf("Longitude: %.6f° %c\n", gps->longitude, gps->ewhemi); printf("Latitude: %.6f° %c\n", gps->latitude, gps->nshemi); printf("Altitude: %.1f meters\n", gps->altitude); printf("UTC Time: %02d:%02d:%02d\n", gps->utc.hour, gps->utc.min, gps->utc.sec); printf("Satellites: %d\n", gps->posslnum); printf("Fix mode: %s\n", gps->fixmode == 1 ? "GPS" : gps->fixmode == 2 ? "DGPS" : "Unknown"); } 修过后该处显示以下错误如何修改[OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c: In function 'gps_parse_nmea': [OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c:174:43: warning: pointer targets in passing argument 1 of 'nmea_str2num' differ in signedness [-Wpointer-sign] [OHOS ERROR] uint8_t calc_check = nmea_str2num(buf); [OHOS ERROR] ^~~ [OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c:27:5: note: expected 'const uint8_t * {aka const unsigned char *}' but argument is of type 'const char *' [OHOS ERROR] int nmea_str2num(const uint8_t *buf, uint8_t *dx) { [OHOS ERROR] ^~~~~~~~~~~~ [OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c:174:30: error: too few arguments to function 'nmea_str2num' [OHOS ERROR] uint8_t calc_check = nmea_str2num(buf); [OHOS ERROR] ^~~~~~~~~~~~ [OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c:27:5: note: declared here [OHOS ERROR] int nmea_str2num(const uint8_t *buf, uint8_t *dx) { [OHOS ERROR] ^~~~~~~~~~~~ [OHOS ERROR] you can check build log in D:\hi3861\hi3861_hdu_iot_application\src\out\hispark_pegasus\wifiiot_hispark_pegasus\build.log [OHOS ERROR] command: "D:\hi3861\hi3861_hdu_iot_application\src\prebuilts\build-tools\win64-x64\bin\ninja.exe -w dupbuild=warn -C D:\hi3861\hi3861_hdu_iot_application\src\out\hispark_pegasus\wifiiot_hispark_pegasus" failed [OHOS ERROR] return code: 1 [OHOS ERROR] execution path: D:\hi3861\hi3861_hdu_iot_application\src scons: *** [src\out\hispark_pegasus\wifiiot_hispark_pegasus\target.elf] Error -1
07-07
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值