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

链接: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 }
时间: 2024-08-15 15:41:31

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

31:字符串中最长的连续出现的字符

31:字符串中最长的连续出现的字符 查看 提交 统计 提问 总时间限制:  1000ms 内存限制:  65536kB 描述 求一个字符串中最长的连续出现的字符,输出该字符及其出现次数.字符串中无空白字符(空格.回车和tab),如果这样的字符不止一个,则输出出现最早的字符. 输入 一行,一个不包含空白字符的字符串,字符串长度小于200. 输出 一行,输出最长的连续出现的字符及其最长的连续出现次数,中间以一个空格分开. 样例输入 aaaaadbbbbbcccccccdddddddddd 样例输出

字符串中连续出现最多的子串 &amp; 字符串中最长重复子串

字符串中连续出现最多的子串 & 字符串中最长重复子串 字符串中连续出现最多的子串 & 字符串中最长重复子串,这两个问题都可以用后缀数组来表示,至于后缀数组可以参考编程珠玑P156:后缀数组就是定义一个数组指针,分别指向字符串中的对应位置,如下: a b c a b c a b c d e .substr[0] b c a b c a b c d e ....substr[1] c a b c a b c d e .......substr[2] a b c a b c d e ......

求一个字串中最长的连续字符串

举例子来说:对于字符串"1234abcdef1234567abcdefghijklmn",这个字串中最长的连续字符串为"abcdefghijklmn". int continumax(char *outputstr,char *inputstr) { char maxrecord[100] = {0}; int maxlength = 0; char currentrecord[100] = {0}; int currentlength = 0; char valu

字符串中连续出现最多的子串 &amp;amp; 字符串中最长反复子串

字符串中连续出现最多的子串 & 字符串中最长反复子串 字符串中连续出现最多的子串 & 字符串中最长反复子串,这两个问题都能够用后缀数组来表示,至于后缀数组能够參考编程珠玑P156:后缀数组就是定义一个数组指针,分别指向字符串中的相应位置,例如以下: a b c a b c a b c d e .substr[0] b c a b c a b c d e ....substr[1] c a b c a b c d e .......substr[2] a b c a b c d e ....

【C】字符串的输入,求输入字符串中最长的单词

首先,基本目标很简单,就是利用C语言:编写一个函数,输入一行字符,将此行字符中的最长的单词输出. 代码如下: #include<stdio.h> void input(char s[]){ int i=0; for(int c;(c=getchar())!='\n';i++){ s[i]=c; } s[i]='\0';//读取完成,记得对这个字符数组封口 } char* findmax(char s[]){ int max=0,word_length=0,p=0,i=0;//这个p是用来记录最

Longest Substring Without Repeating Characters 字符串中最长的无重复子串长度

Longest Substring Without Repeating Characters Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the lengt

找出一个字符串中最长重复次数的子字符串,并计算其重复次数

原题 找出一个字符串中最长重复次数的子字符串,并计算其重复次数.例如:字符串"abc fghi bc kl abcd lkm abcdefg",并返回"abcd"和2. 我的思路 为了方便表述,我们使用变量src作为原字符串,sub_str作为子字符串. 由于题目要求寻找至少重复2次的最长的子字符串,重点在于最长的子字符串,而不在于重复的最多次数.因此我们可以从长度最长的字符串入手,计算其重复次数.只要重复达到2次,即可返回该字符串. 显然长度最长的子字符串就是原串

找出两个字符串中最长的相同子字符串

//找出两个字符串中最长的相同子字符串 public class Stringdemo { public static void main(String[] args) { String str1 = new String("eeabcde"); String str2 = new String("bcdefabcabcdedegg"); byte[] char1 = str1.getBytes(); byte[] char2 = str2.getBytes();

字符串中最长的不重合字串长度

例子 "abmadsefadd"  最长长度为5 "avoaid"           最长长度为3 思路 空间换时间hashTable,标准下其实位置beg.初始化全局最大值0.开辟字符数组,起初标为0. 访问数组时 如果该字符在hashTable对应的哈希值为1,则计算当前位置到beg的距离,并且把beg赋值为当前位置.如果大于全局最大值,则替换全局最大值 如果该字符在hashTable对应的哈希值为0,则置1 参考代码 #include <iostrea