输入一个长度为
、由数字和小写字母混合构成的字符串
。保证至少存在一个数字子串。
记最长的数字子串长度为
,有
个长度为
的数字子串。在一行上先首尾相连的输出
个长度为
的数字子串(不使用空格分割),随后输出一个逗号,再输出
。
abcd12345ed125ss123058789
123058789,9
11a22b33c
112233,2
在这个样例中,数字子串
长度均为
,都是最长的数字子串。
本题数据已规范为单组询问(2025/01/15)。
#include <stdio.h>
#include <string.h>
//要不遍历两次吧。。。。这样应该很简单了
int main() {
char string[201];
scanf("%s",string);
int max_length=1;//最长子串的长度
int l=strlen(string);
for (int i=0; i<l;i++ ) {
if (string[i]>='0'&&string[i]<='9') {
int zi_length=1;
for (int j=1; j<l; j++) {
if(string[i+j]>='0'&&string[i+j]<='9')
{
zi_length++;
}else {
//遇到字母则跳过已经遍历的子串
i=i+j-1; //因为后面i还需要加加一次
break;
}
}
max_length=zi_length>max_length?zi_length:max_length; //记录最长的
}
}
//printf("zuichang%d",max_length);
//重新遍历一次把最长的子串输出,要么你就空间换时间
for (int i=0; i<l;i++ ) {
if (string[i]>='0'&&string[i]<='9') {
int zi_length=1;
int start_i=i; //记录本子串的初始位置
for (int j=1; j<l; j++) {
if(string[i+j]>='0'&&string[i+j]<='9')
{
zi_length++;
}else {
//遇到字母则跳过已经遍历的子串
i=i+j-1; //因为后面i还需要加加一次
break;
}
}
//如果这个子串长度等于前面找到的最长子串则输出它
if (zi_length==max_length) {
for (int j=0; j<zi_length; j++) {
printf("%c",string[start_i+j]);//要初始位置因为i会变
}
}
}
}
printf(",%d",max_length);
return 0;
} #include <stdio.h>
#include <string.h>
#define max(a, b) ((a) > (b) ? (a) : (b))
int main()
{
char str[201] = {0};
int i, j, maxl = 0;
scanf("%s", str);
int dp[strlen(str)];
dp[0] = str[0] >= '0' && str[0] <= '9' ? 1 : 0;
for (i = 1; i < strlen(str); i++)
{
dp[i] = 0;
if (str[i] >= '0' && str[i] <= '9')
{
dp[i] = dp[i - 1] + 1;
maxl = max(maxl, dp[i]);
}
}
for (i = 0; i < strlen(str); i++)
{
if (dp[i] == maxl)
{
for (j = i - maxl + 1; j <= i; j++)
{
printf("%c", str[j]);
}
}
}
printf(",%d", maxl);
return 0;
} 昨天刚做的最长上升子序列,属于是路径依赖了
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char input[200]={0};
int max=0,flag=0,count=0,i=0,j=0;
while(scanf("%s", input)!=EOF){
for(i=0; i<strlen(input); i++){
if((isdigit(input[i]) && !isdigit(input[i-1]))|| (isdigit(input[0]))){
for(j=i; isdigit(input[j])&&j<strlen(input); j++)
{
count++;
}
max = max>count?max:count;
i += count;
count = 0;
}
}
for(i=0; i<strlen(input); i++){
if((isdigit(input[i]) && !isdigit(input[i-1]))|| (isdigit(input[0]))){
flag = i;
for(j=i; isdigit(input[j])&&j<strlen(input); j++)
{
count++;
}
if(count == max){
for(int k=i; k<j; k++)
printf("%c", input[k]);
}
i += count;
count = 0;
}
}
printf(",%d\n", max);
max = 0;
}
return 0;
} #include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
int count = 0;
int i, len;
char str[201]= {0};
char max_candi[201] = {0};
while (scanf("%s", str) != EOF) {
len = strlen(str);
char *p = str;
/* 分割字符串 */
for(i = 0; i < len; i++)
{
if (p[i] < '0' || p[i] > '9') {
p[i] = '\0';
}
}
/* 遍历数字字符串,记录每段的起始位置i和该段的长度 */
int max = 0;
for(i = 0; i < len; i++)
{
if (p[i] >= '0' && p[i] <= '9') {
max_candi[i] = strlen(p+i);//易错点
max = (max > max_candi[i]) ? max : max_candi[i];
i+=strlen(p+i) - 1;//易错点
}
}
/* 遍历输出 */
for(i = 0; i < len; i++)
{
if(max_candi[i] == max)
{
printf("%s", p+i);
}
}
printf(",%d\n", max);
memset(str,0,201*sizeof(char));
}
return 0;
} #include <stdio.h>
#include <string.h>
int main()
{
char str[200]={0}, num[200]={0};
int i, j, k, count, max;
while(scanf("%s", &str)!=EOF)
{
i=0;
max=0;
k=0;
while(i<strlen(str))
{
count=0;
while(str[i]>='0' && str[i]<='9')
{
count++;
i++;
}
if(count>=max)
{
if(count>max) k=0; //如果新的最大值比之前的最大值大,把输出子串清空
max=count>max ? count : max;
for(j=0;j<max;j++) //如果新的最大值等于之前的最大值,输出子串不清空
{
num[k]=str[i-count+j];
k++;
}
num[k]='\0'; //输出子串最后加个空字符
}
i++;
}
printf("%s,%d\n",num,max);
}
return 0;
} #include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define N 100
int main()
{
char s[N] = {0};
while(scanf("%s", s) != EOF){
int len = strlen(s);
int maxLen = 0;
char res[N] = {0};
char temp[N] = {0};
int cnt = 0, tempCnt = 0;
s[len++] = 'a';
for(int i = 0; i < len; i++){
if(s[i] >= '0' && s[i] <= '9'){
temp[tempCnt++] = s[i];
}else{
if(tempCnt > maxLen){
maxLen = tempCnt;
cnt = 0;
for(int j = 0; j < tempCnt; j++){
res[cnt++] = temp[j];
}
res[cnt++] = '\0';
tempCnt = 0;
}else if(tempCnt == maxLen && tempCnt > 0){
cnt--;
for(int j = 0; j < tempCnt; j++){
res[cnt++] = temp[j];
}
res[cnt++] = '\0';
tempCnt = 0;
}else{
tempCnt = 0;
}
}
}
printf("%s,%d\n", res, maxLen);
}
return 0;
} 一遍while找最长值,一边while打印
#include
#include
int main()
{
char str[201] = {0}, *beg, *end;
int max = 0;
while (scanf("%s", str) != EOF) {
beg = end = str;
max = 0;
while (*end) {
if (!isdigit(*end)) {
if (max < (end - beg))
max = end - beg;
beg = end + 1;
}
end++;
}
if (max < (end - beg))
max = end - beg;
beg = end = str;
while (*end) {
if (!isdigit(*end)) {
if (max == (end - beg)) {
*end = 0;
printf("%s", beg);
}
beg = end + 1;
}
end++;
}
if (max == (end - beg))
printf("%s", beg);
printf(",%d\n", max);
}
return 0;
}
#include <stdio.h>
#define N 200
int main()
{
char str[N];
int pos[N],i=0,j,cnt,len,flag,max_len,start;
while(gets(str))
{
len=0;flag=0;max_len=0;cnt=0;
while(str[i]!='\0')
{
if(str[i]>='0'&&str[i]<='9')
{
if(flag==0)
{
start=i;
flag=1;
}
len++;
}
else
{
if(flag)
{
if(len>max_len)
{
cnt=0;
max_len=len;
pos[cnt++]=start;
}
else if(len==max_len)
{
pos[cnt++]=start;
}
flag=0;
len=0;
}
}
i++;
}
if(flag)
{
if(len>max_len)
{
cnt=0;
max_len=len;
pos[cnt++]=start;
}
else if(len==max_len)
{
pos[cnt++]=start;
}
}
for(i=0;i<cnt;i++)
{
for(j=pos[i];j<pos[i]+max_len;j++)
{
printf("%c",str[j]);
}
}
printf(",%d\n",max_len);
}
return 0;
} #define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
int main()
{
char a[250] = { 0 };
int x[250] = { 0 };
int s, i, j,k=0,sum=0;
while (gets(a) != NULL)
{
k = 0;
int flag = 0;
s = strlen(a);
sum = 0;
for (i = 0; i < s; i++)
{
if (a[i] >= '0' && a[i] <= '9')
{
if (flag == 0)
{
k = i;
flag = 1;
}
x[k]++;
}
else
{
flag = 0;
}
}
int max = 0;
for (i = 0; i < 250; i++)
{
if (x[i] > max)
max = x[i];
}
for (i = 0; i < 250; i++)
{
if (x[i] == max)
{
for (j = i; j < i + max; j++)
printf("%c", a[j]);
}
}
printf(",%d\n", max);
memset(a, 0, sizeof(a));
memset(x, 0, sizeof(x));
}
return 0;
} #include <stdio.h>
#include <stdlib.h>
typedef unsigned char u8;
u8 isnum(char c)
{
return (c>'9'||c<'0')?0:1;
}
u8 getnumlen(char* str)
{
u8 i;
for(i=0;;i++)
if(!isnum(str[i]))
return i;
return i-1;
}
#define RESET_ALL \
memset(str,0,sizeof(str));\
memset(len,0,sizeof(len));\
memset(max_index,0,sizeof(max_index));\
max = 0;\
i = 0;\
j = 0;\
k = 0;
#define RESET_J \
memset(max_index,0,sizeof(max_index));\
j=0;
int main()
{
char str[201];
u8 len[201];
u8 max_index[101];
u8 max;
int i,j,k;
RESET_ALL;
while(EOF!=scanf("%s",str))
{
for(i=0;str[i]!=0;)
{
if(isnum(str[i]))
{
len[i]=getnumlen(&str[i]);
if(max<len[i])
{
RESET_J;
max = len[i];
max_index[j] = i;
j++;
}
else if(max==len[i])
{
max_index[j] = i;
j++;
}
i+=len[i];
}
else i++;
}
for(i=0;i<j;i++)
for(k=0;k<max;k++)
printf("%c",str[max_index[i]+k]);
printf(",%d\n",max);
RESET_ALL;
}
return 0;
}
// 使用字符串数组qsort排序
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *g_array[100];
int g_index = 0;
int GetIndex(char *s)
{
for (int i = 0; i < 100; i++) {
if (g_array[i] != NULL && strcmp(s, g_array[i]) == 0) {
return i;
}
}
return -1;
}
void InsertArray(char *s)
{
int i = GetIndex(s);
// 字符串已存在不再插入
if (i >= 0) {
return;
}
g_array[g_index] = s;
g_index++;
}
void FreeArray(void)
{
for (int i = 0; i < g_index; i++) {
if (g_array[i] != NULL) {
free(g_array[i]);
g_array[i] = NULL;
}
}
}
int Cmp(const void *p1, const void *p2)
{
char *a = *(char **)p1;
char *b = *(char **)p2;
// 按字符串长度从小到大排序
return strlen(b) - strlen(a);
}
int main(void)
{
char str[1000] = {'\0'};
while (gets(str)) {
// 多组数组需要在每次循环初始化全局变量
memset(g_array, 0, sizeof(g_array));
g_index = 0;
int i = 0;
while (i < strlen(str)) {
if (str[i] >= '0' && str[i] <= '9') {
int len = 0;
int startIndex = i;
while (i < strlen(str)) {
// 计算数字长度
if (str[i] >= '0' && str[i] <= '9') {
len++;
i++;
// 处理字符末尾为数字情况
if (str[i] == '\0') {
char *tmp = (char *)malloc(sizeof(char) * (len + 1));
strncpy(tmp, &str[startIndex], len);
tmp[len] = '\0';
InsertArray(tmp);
}
} else {
// 复制纯数字字符串到字符串数组
char *tmp = (char *)malloc(sizeof(char) * (len + 1));
strncpy(tmp, &str[startIndex], len);
tmp[len] = '\0';
InsertArray(tmp);
break;
}
}
} else {
i++;
}
}
qsort(g_array, g_index, sizeof(char *), Cmp);
for (int i = 0; i < g_index; i++) {
// 处理字符串长度相等情况
if (strlen(g_array[i]) == strlen(g_array[0])) {
printf("%s", g_array[i]);
}
}
printf(",%d\n", strlen(g_array[0]));
FreeArray();
}
}