目录
A - 雷同检测
考试的时候老师最讨厌有人抄袭了。自从有了电子评卷,老师要查找雷同卷,就容易多了,只要将两个人的答案输入计算机,进行逐个字符的比对,把相同的位置都找出来,就一目了然了。
输入格式
2 行,每行包含一串字符(长度不超过 200)。
输出格式
11 行,包含若干个以空格分隔的数字,表示出现相同字符的位置。
Sample Input
I am suantoujun. I am huayemei.
Sample Output
1 2 3 4 5 6 8 9
#include <stdio.h>
#include <string.h>
char a[210],b[210];
int main()
{
int m,n,i,t;
gets(a);
gets(b);
m=strlen(a);
n=strlen(b);
for (i=0;i<m&&i<n;i++) {
if (a[i]==b[i]) {
printf("%d ",i+1);
}
}
return 0;
}
B - 首字母大写
对一个字符串中的所有单词,如果单词的首字母不是大写字母,则把单词的首字母变成大写字母。在字符串中,单词之间通过空白符分隔,空白符包括:空格(' ')、制表符('\t')、回车符('\r')、换行符('\n')。
Input
输入一行:待处理的字符串(长度小于80)。
Output
输出一行:转换后的字符串。
Sample Input
if so, you already have a google account. you can sign in on the right.
Sample Output
If So, You Already Have A Google Account. You Can Sign In On The Right.
#include <stdio.h>
#include <string.h>
int main()
{
char a[100];
gets(a);
int i,n=strlen(a);
if (a[0]>='a'&&a[0]<='z') {
a[0]-=32;
}
for (i=1;i<n;i++) {
if ((a[i]>='a'&&a[i]<='z')&&(a[i-1]==' '||a[i-1]=='\t'||a[i-1]=='\r'||a[i-1]=='\n')) {
a[i]-=32;
}
}
for (i=0;i<n;i++) {
printf("%c",a[i]);
}
return 0;
}
C - 大小写转换
读入一些字符串,将其中的小写字母转成大写字母(其他字符不变)。
输入
输入为多行,每行为一个字符串,字符串只由字母和数字组成,长度不超过80。输入以“End of file”结束。
输出
对于每行输入,输出转换后的字符串。
输入示例
Hello ICPC2004 12345abcde
输出示例
HELLO ICPC2004 12345ABCDE
提示
用“scanf("%s", str) == 1”这个条件可以判断输入是否结束。如果此条件为假,则输入结束(对于本题)。
#include <stdio.h>
#include <string.h>
int main()
{
char a[100];
int n,i;
while (scanf("%s",a)==1) {
n=strlen(a);
for (i=0;i<n;i++) {
if (a[i]>='a'&&a[i]<='z') {
a[i]-=32;
}
}
for (i=0;i<n;i++) {
printf("%c",a[i]);
}
printf("\n");
}
return 0;
}
D - 数字反转
给定一个整数,请将该数各个位上数字反转得到一个新数。新数也应满足整数的常见形式,即除非给定的原数为零,否则反转后得到的新数的最高位数字不应为零(参见样例2)。
输入格式
输入共 11 行,一个整数 NN。
输出格式
输出共 1 行,一个整数,表示反转后的新数。
数据范围
−1,000,000,000≤N≤1,000,000,000。
Sample Input
123
Sample Output
321
Sample Input 2
-380
Sample Output 2
-83
#include <stdio.h>
#include <string.h>
int main()
{
char a[20];
scanf("%s",&a);
if (a[0]=='-') {
int n,i,t;
printf("%c",a[0]);
n=strlen(a);
for (i=n-1;i>=1;i--) {
if (a[i]!='0') {
break;
}
}
for (t=i;t>=1;t--) {
printf("%c",a[t]);
}
} else {
int n,i,t;
n=strlen(a);
for (i=n-1;i>=0;i--) {
if (a[i]!='0') {
break;
}
}
for (t=i;t>=0;t--) {
printf("%c",a[t]);
}
}
return 0;
}
E - 删除单词后缀
给定一个单词,如果该单词以er
、ly
或者ing
后缀结尾, 则删除该后缀(题目保证删除后缀后的单词长度不为 0),否则不进行任何操作。
输入格式
输入一行,包含一个单词(单词中间没有空格,每个单词最大长度为 32)。
输出格式
输出按照题目要求处理后的单词。
Sample Input
referer
Sample Output
refer
#include <stdio.h>
#include <string.h>
int main()
{
char a[50];
int n,i;
scanf("%s",a);
n=strlen(a);
if ((a[n-1]=='r'&&a[n-2]=='e')||(a[n-1]=='y'&&a[n-2]=='l')) {
for (i=0;i<n-2;i++) {
printf("%c",a[i]);
}
} else if (a[n-1]=='g'&&a[n-2]=='n'&&a[n-3]=='i') {
for (i=0;i<n-3;i++) {
printf("%c",a[i]);
}
} else {
printf("%s",a);
}
return 0;
}
F - 判断字符串是否为回文
输入一个字符串,输出该字符串是否回文。回文是指顺读和倒读都一样的字符串。
输入格式
输入为一行字符串(字符串中没有空白字符,字符串长度不超过 100)。
输出格式
如果字符串是回文,输出"yes"
;否则,输出"no"
。
Sample Input
abcdedcba
Sample Output
yes
#include <stdio.h>
#include <string.h>
int main()
{
char a[110],b[110];
int n,i,t=0;
scanf("%s",a);
n=strlen(a);
for (i=n-1;i>=0;i--) {
b[t]=a[i];
t++;
}
b[t]='\0';
if (strcmp(a,b)==0) {
printf("yes");
} else {
printf("no");
}
return 0;
}
G - 基础数据结构——栈(1)
给你一串字符,不超过50个字符,可能包括括号、数字、字母、标点符号、空格,你的任务是检查这一串字符中的( ) ,[ ],{ }是否匹配。
Input
输入数据有多组,每组数据不超过100个字符并含有( ,) ,[, ],{, }一个或多个。处理到文件结束。
Output
如果匹配就输出“yes”,不匹配输出“no”
Sample Input
sin(20+10)
{[}]
Sample Output
yes
no
#include <stdio.h>
#include <string.h>
int main()
{
char a[110],b[110];
while (gets(a)) {
int n,i,count=0,sign=0;
n=strlen(a);
for (i=0;i<n;i++) {
if (a[i]=='('||a[i]=='['||a[i]=='{') {
count++;
b[count]=a[i];//入栈
sign=1;
} else if (a[i]==')'||a[i]==']'||a[i]=='}') {
if ((a[i]==')'&&b[count]=='(')||(a[i]==']'&&b[count]=='[')||(a[i]=='}'&&b[count]=='{')) {
count--;//出栈
} else {
sign=0;
break;
}
}
}
if (sign==1&&count==0) {
printf("yes\n");
} else {
printf("no\n");
}
}
return 0;
}
H - 字典序
给你两个不同的字符串,如果第一个字符串的字典序小于第二个字符串,则输出YES,如果第一个字符串的字典序大于第二个字符串,则输出NO。
Input
两行。第一行一个字符串,第二行一个字符串。保证字符串的长度不超过10000。保证两个字符串不完全相等。
Output
如果第一个字符串的字典序小于第二个字符串,则输出YES,如果第一个字符串的字典序大于第二个字符串,则输出NO。
Sample Input
abc abe
Sample Output
YES
#include <stdio.h>
#include <string.h>
int main()
{
char a[10010],b[10010];
gets(a);
gets(b);
if (strcmp(a,b)<0) {
printf("YES");
} else {
printf("NO");
}
return 0;
}
I - 验证子串
输入两个字符串,验证其中一个串是否为另一个串的子串。
输入格式
输入两个字符串, 每个字符串占一行,长度不超过 200 且不含空格。
输出格式
若第一个串 s1 是第二个串 s2 的子串,则输出"(s1) is substring of (s2)"
;
否则,若第二个串 s2是第一个串s1的子串,输出"(s2) is substring of (s1)"
;
否则,输出"No substring"
。
Sample Input
abc dddncabca
Sample Output
abc is substring of dddncabca
#include <stdio.h>
#include <string.h>
int main()
{
char a[210],b[210];
scanf("%s",a);
scanf("%s",b);
if (strstr(a,b)) {
printf("%s is substring of %s",b,a);
} else if (strstr(b,a)) {
printf("%s is substring of %s",a,b);
} else {
printf("No substring");
}
return 0;
}
J - 子串查找
题目描述
这是一道模板题。
给定一个字符串 A 和一个字符串 B,求 B 在 A 中的出现次数。A 和 B 中的字符均为英语大写字母或小写字母。
A 中不同位置出现的 B 可重叠。
输入格式
输入共两行,分别是字符串 A 和字符串 B。
输出格式
输出一个整数,表示 B 在 A 中的出现次数。
样例
Input | Output |
zyzyzyz zyz | 3 |
数据范围与提示
1≤A,B 的长度 ≤10^6,A、B 仅包含大小写字母。
这题由于字符串长度较长,采用暴力的方法会造成超时,所以使用kmp算法解决
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e6+10;
char a[maxn],b[maxn];
int Next[maxn];
void getnext(int n) {
int i,t;
Next[0]=-1;
i=0,t=-1;
while (i<n-1) {
if (t==-1||b[i]==b[t]) {
t++;
i++;
Next[i]=t;
} else {
t=Next[t];
}
}
}
int kmp(void) {
int n=strlen(b);
int m=strlen(a);
getnext(n);
int i=0,t=0,count=0;
while (i<m) {
if (t==-1||a[i]==b[t]) {
i++;
t++;
} else {
t=Next[t];
}
if (t==n) {
count++;
t--;
i--;
t=Next[t];
}
}
return count;
}
int main()
{
scanf("%s",a);
scanf("%s",b);
int x=kmp();
printf("%d",x);
return 0;
}
K - 剪花布条
题目描述
一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案。对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢?
输入格式
输入数据为多组数据,读取到 #
字符时结束。每组数据仅有一行,为由空格分开的花布条和小饰条。花布条和小饰条都是用可见 ASCII 字符表示的,不会超过 10001000 个字符。
注意:这个 #
应为单个字符。若某字符串开头有 #
,不意味着读入结束!
输出格式
对于每组数据,输出一行一个整数,表示能从花纹布中剪出的最多小饰条个数。
样例
Input | Output |
abcde a3 aaaaaa aa # | 0 3 |
数据范围与提示
对于全部数据,字符串长度 ≤1000。
与上一题不同,这一题没有重叠部分,且字符串在、长度较短,可以采用暴力的方法通过
#include <stdio.h>
#include <string.h>
int main()
{
char a[1010],b[1010];
while (1) {
scanf("%s",a);
if (strcmp(a,"#")==0) {
break;
}
scanf("%s",b);
int n,i,t=0,m,j,k,sign=0,count=0;
n=strlen(a);
m=strlen(b);
for (i=0;i<n;i++) {
if (a[i]==b[t]) {
k=i;//记录i的位置
sign=1;
for (t=1;t<m;t++) {
i++;
if (a[i]!=b[t]) {
sign=0;
break;
}
}
if (sign==1) {
count++;//匹配成功后继续向后匹配下一个
} else if (sign==0) {
i=k;//未成功则回到原来的位置
}
t=0;
}
}
printf("%d\n",count);
}
return 0;
}
L - 最长回文子串
输入一个字符串Str,输出Str里最长回文子串的长度。
回文串:指aba、abba、cccbccc、aaaa这种左右对称的字符串。
串的子串:一个串的子串指此(字符)串中连续的一部分字符构成的子(字符)串
例如 abc 这个串的子串:空串、a、b、c、ab、bc、abc
Input
输入Str(Str的长度 <= 1000)
Output
输出最长回文子串的长度L。
Sample Input
daabaac
Sample Output
5
字符串长度不大,依旧可以采用暴力
#include <stdio.h>
#include <string.h>
char a[1010];
int panduan(int i,int t) {
while (i<t) {
if (a[i]!=a[t]) {
return 0;
}
i++;
t--;
}
return 1;
}
int main()
{
int n,i,t,max=-1;
scanf("%s",a);
n=strlen(a);
for (i=0;i<n;i++) {
for (t=n-1;t>i;t--) {
if (panduan(i,t)==0) {
continue;
} else {
if ((t-i+1)>max) {
max=t-i+1;
}
}
}
}
printf("%d",max);
return 0;
}