字符串中最长的连续出现的字符【指针练习】

链接:http://ica.openjudge.cn/zz/2/

总时间限制: 1000ms  内存限制: 65536kB
描述

求一个字符串中最长的连续出现的字符,输出该字符及其出现次数,字符串中无空白字符(空格、回车和tab),如果这样的字符不止一个,则输出第一个

输入
首先输入N,即测试数据的组数
每组测试数据输入:
一行,一个不包含空白字符的字符串,字符串长度小于200
输出
一行,输出最长的连续出现的字符及其出现次数,中间用空格隔开
样例输入
2
aaaaabbbbbcccccccdddddddddd
abcdefghigk
样例输出
d 10
a 1

代码一:不用指针 

复制代码
 1 #include<stdio.h>
 2 #include<string.h>
 3 int main(int argc, char *argv[])
 4 {
 5     int n,i,j,k,maxIndex;
 6     char str[205];
 7     char count1[205];
 8     int  count2[205];
 9     
10     freopen("data.in","r",stdin);
11     scanf("%d",&n);
12     for(i=0;i<n;i++)
13     {
14         scanf("%s",str);
15         //memset(count1,' ',sizeof(count1));
16         //memset(count2,0,sizeof(count2));
17         j=0;
18         count1[j]=str[0];
19         count2[j]=1;
20         for(k=1;str[k]!='\0';k++)
21         {
22             if(str[k]==count1[j])
23             {
24                 count2[j]++;
25             }
26             else
27             {
28                 j++;
29                 count1[j]=str[k];
30                 count2[j]=1;
31             }
32         }
33         
34         maxIndex=0;
35         for(k=1;k<=j;k++)
36         {
37             if(count2[k]>count2[maxIndex])
38             {
39                 maxIndex=k;
40             }
41         }
42         printf("%c %d\n",count1[maxIndex],count2[maxIndex]);
43     }
44     return 0;
45 }
复制代码

代码二:用指针和链表。头插法构造链表  

复制代码
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 struct obj
 4 {
 5     char ch;
 6     int num;
 7     struct obj *next;
 8 };
 9 int main(int argc, char *argv[])
10 {
11     int n,i,j,k;
12     char str[205];
13     struct obj *head,*temp,*p;
14     char maxCh;
15     int maxNum;
16     freopen("data.in","r",stdin);
17     scanf("%d",&n);
18     for(i=0;i<n;i++)
19     {
20         scanf("%s",str);
21         
22         head=temp=p=NULL;
23         
24         temp=(struct obj *)malloc(sizeof(struct obj));
25         temp->ch=str[0];
26         temp->num=1;
27         temp->next=NULL;
28         head=temp;
29         for(j=1;str[j]!='\0';j++)
30         {
31             if(str[j]==head->ch) head->num=head->num+1;
32             else
33             {
34                 temp=(struct obj *)malloc(sizeof(struct obj));
35                 temp->ch=str[j];
36                 temp->num=1;
37                 temp->next=NULL;
38                 
39                 temp->next=head;
40                 head=temp;
41             }
42         }
43         
44         
45         p=head;
46         maxCh=head->ch;
47         maxNum=head->num;
48         while(p!=NULL)
49         {
50             if(p->num>=maxNum)//注意:这里要有等号,因为题目要求多个相等值选第一个输出。这里是头插法,选最后一个输出即可。 
51             {
52                 maxNum=p->num;
53                 maxCh=p->ch;
54             }
55             p=p->next;
56         }
57         printf("%c %d\n",maxCh,maxNum);
58         
59         p=head;
60         while(p!=NULL)
61         {
62             temp=p;
63             p=p->next;
64             free(temp);
65         }
66     }
67     return 0;
68 }
复制代码

 

posted on   华山青竹  阅读(1555)  评论(0编辑  收藏  举报

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示