向前走莫回头❤

【openjudge 计算概论(A)】[编程练习(字符串)]

1:统计字符数

#include<cstdio>
#include<cstring>
using namespace std;
char ch[1010];
int num[150],n,len;
int main()
{
	int i;
	scanf("%d",&n);
	while(n--)
	 {
	 	getchar();
	 	memset(num,0,sizeof(num));
	 	int maxn=0; char l=128;
	 	scanf("%s",ch);
	 	len=strlen(ch);
	 	for(i=0;i<len;++i)
	 	 {
	 	 	num[ch[i]]++;
	 	 	if(num[ch[i]]>maxn) maxn=num[ch[i]],l=ch[i];
	 	 	if(num[ch[i]]==maxn&&ch[i]<l) l=ch[i]; 
		  }
		printf("%c %d\n",l,maxn);
		getchar();
	 }
	return 0;
}

2:单词倒排

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char s[110],ch[110][110];
int tot,len;
int main()
{
	int i,j;
	gets(s+1);
	len=strlen(s+1);
	s[++len]=' ';
	i=1;
	while(i<=len)
	 {
	 	tot++; j=0;
	 	while(s[i]!=' ') ch[tot][j++]=s[i++];
	 	i++;
	 }
	for(i=tot;i>0;--i) printf("%s ",ch[i]);
	return 0;
}

3:过滤多余的空格

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
char s[210][210];
int len;
int main()
{
	int i=0;
	while (cin>>s[++len]);
	len--;
	for (i=1;i<=len;i++)
	  cout<<s[i]<<' ';
	return 0;
} 

4:最长单词2

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node{
	char ch[510];
	int ln;
}a[510];
char s[510];
int len,tot,maxn;
int main()
{
	int i,j;
	gets(s+1);
	len=strlen(s+1);
	i=1; s[len]=' ';
	while(i<=len)
	 {
	 	tot++; j=0;
	 	while(s[i]!=' ') a[tot].ch[j++]=s[i++];
	 	a[tot].ln=strlen(a[tot].ch);
	 	if(a[tot].ln>a[maxn].ln) maxn=tot;
	 	i++;
	  } 
	printf("%s",a[maxn].ch);
	return 0;
 } 

5:单词排序

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
string s[110];
int tot,len;
int main()
{
	int i,j;
	while(cin>>s[++tot]);
	tot--;
    sort(s+1,s+tot+1);
	for(int i=1;i<=tot;++i) 
	 if(s[i]==s[i-1]) continue;
	  else cout<<s[i]<<endl;
	return 0;
}

6:字符串最大跨距

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char s[310],s1[15],s2[15];
int len,l1,l2;
int main()
{
	int i,j,last;
	gets(s+1);
	len=strlen(s+1);
	i=1;
	while(i<=len)
	 {
	 	while(s[i]!=',') ++i;
	 	j=0; last=i-1; i++;
	 	while(s[i]!=',') s1[++j]=s[i++];
	 	i++; j=0;
	 	while(i<=len) s2[++j]=s[i++];
	 }
	l1=strlen(s1+1); l2=strlen(s2+1);
	len=last;
	bool p1=0;
	for(i=1;i<=len;++i)
	 if(s[i]==s1[1])
	  {
	  	int x=i+1,y=2;
	  	while(x<=len&&y<=l1)
	  	 if(s[x]==s1[y]) x++,y++;
	  	  else break;
	  	if(y>l1) {p1=1; break; }
	  }
	int a;
	if(p1) a=i+l1-1; 
	int p2=0;
	for(i=len;i>=a+l1;--i)
	 if(s[i]==s2[l2])
	  {
	  	int x=i-1,y=l2-1;
	  	while(x&&y)
	  	 if(s[x]==s2[y]) --x,--y;
	  	  else break;
	  	if(!y) {p2=1; break;}
	  }
	int b;
	if(!p1||!p2) printf("-1\n");
	 else 
	  {
	  	b=i-l2+1;
	  	printf("%d\n",b-a-1);
	  }
	return 0;
}



posted @ 2016-10-20 21:32  lris0-0  阅读(87)  评论(0编辑  收藏  举报
过去的终会化为美满的财富~o( =∩ω∩= )m