【openjudge 计算概论(A)】[编程练习(字符串)]
来源:互联网 发布:淘宝发布宝贝找不到 编辑:程序博客网 时间:2024/04/30 10:49
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;}
0 0
- 【openjudge 计算概论(A)】[编程练习(字符串)]
- 【openjudge 计算概论(A)】[编程练习(数组练习)]
- 【openjudge 计算概论(A)】[基础编程练习2]
- 【openjudge 计算概论(A)】[基础编程练习1]
- 【openjudge 计算概论(A)】[指针练习]
- 【openjudge 计算概论(A)】[ 函数与字符串练习(2)]
- 【openjudge 计算概论(A)】[函数与字符串练习(1)]
- 【openjudge 计算概论(A)】[函数递归练习(4)]
- 【openjudge 计算概论(A)】[函数递归练习(3)]
- 【openjudge 计算概论(A)】[函数递归练习(2)]
- 【openjudge 计算概论(A)】[函数递归练习(1)]
- 【openjudge 计算概论(A)】[基础编程练习(控制成分)]
- 【openjudge 计算概论(A)】[基础编程练习(运算成分)]
- 【openjudge 计算概论(A)】[基础编程练习(数据成分)]
- openjudge 计算概论基础编程练习(控制成分)
- openjudge 计算概论 函数与字符串练习(1)2:字符串排序
- 【openjudge 计算概论(A)】[结构体与链表练习]
- 计算概论(A) / 函数与字符串练习(2)2:DNA排序
- ModelDriven
- 10步开发一个QNX程序
- 大数据工程师必备技能图谱
- 51nod 1091 线段的重叠(贪心思维)
- 以人为本中的“本”是指
- 【openjudge 计算概论(A)】[编程练习(字符串)]
- 算法题型的个人练习_02
- paramsPrepareParamsStack(源码详解)
- Android Studio com.android.dex.DexException: Multiple dex files define(重复引用包)
- 有关Java面试的一般问题 汇集
- 时间效率与空间效率的平衡-面试题36-数组中的逆序对
- 容器类的一些笔记
- larvael---(I)---数据和模型之数据连接层
- LA6910Cutting Tree 逆向并查集