A - 雷同检测
考试的时候老师最讨厌有人抄袭了。自从有了电子评卷,老师要查找雷同卷,就容易多了,只要将两个人的答案输入计算机,进行逐个字符的比对,把相同的位置都找出来,就一目了然了。
输入格式
22 行,每行包含一串字符(长度不超过 200200)。
输出格式
11 行,包含若干个以空格分隔的数字,表示出现相同字符的位置。
Sample Input
I am suantoujun. I am huayemei.
Sample Output
1 2 3 4 5 6 8 9
#include <stdio.h>
char a[210], b[210];
int main() {
scanf("%[^\n]", a);
getchar();
scanf("%[^\n]", b);
for (int i = 0; a[i] != '\0'; 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>
char a[85];
int main() {
gets(a);
if (a[0] >= 'a' && a[0] <= 'z')
a[0] -= 32;
for (int i = 1; i < strlen(a) - 1; i++)
if (a[i] == ' ') {
i++;
if (a[i] >= 'a' && a[i] <= 'z')
a[i] -= 32;
}
printf("%s",a);
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[90];
while(scanf("%s",a)==1){
for(int i=0;i<strlen(a);i++)
if(a[i]>='a'&&a[i]<='z')
a[i]-=32;
printf("%s\n",a);
}
return 0;
}
D - 数字反转
给定一个整数,请将该数各个位上数字反转得到一个新数。新数也应满足整数的常见形式,即除非给定的原数为零,否则反转后得到的新数的最高位数字不应为零(参见样例 22)。
输入格式
输入共 11 行,一个整数 NN。
输出格式
输出共 1 行,一个整数,表示反转后的新数。
数据范围
-1,000,000,000 \le N \le 1,000,000,000−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>
int main()
{
int n,a=0;
scanf("%d",&n);
while(n){
a=a*10+n%10;
n/=10;
}
printf("%d",a);
return 0;
}
E - 删除单词后缀
给定一个单词,如果该单词以er
、ly
或者ing
后缀结尾, 则删除该后缀(题目保证删除后缀后的单词长度不为 00),否则不进行任何操作。
输入格式
输入一行,包含一个单词(单词中间没有空格,每个单词最大长度为 3232)。
输出格式
输出按照题目要求处理后的单词。
Sample Input
referer
Sample Output
refer
#include<stdio.h>
#include<string.h>
char s[101];
int main() {
scanf("%s", s);
int a = strlen(s);
if (s[a - 3] == 'i' && s[a - 2] == 'n' && s[a - 1] == 'g')
s[a - 3] = '\0';
else if (s[a - 2] == 'e' && s[a - 1] == 'r')
s[a - 2] = '\0';
else if (s[a - 2] == 'l' && s[a - 1] == 'y')
s[a - 2] = '\0';
printf("%s", s);
return 0;
}
F - 判断字符串是否为回文
输入一个字符串,输出该字符串是否回文。回文是指顺读和倒读都一样的字符串。
输入格式
输入为一行字符串(字符串中没有空白字符,字符串长度不超过 100100)。
输出格式
如果字符串是回文,输出"yes"
;否则,输出"no"
。
Sample Input
abcdedcba
Sample Output
yes
#include <stdio.h>
#include <string.h>
char s[101];
int main() {
scanf("%s", s);
int a = strlen(s);
for (int i = 0; i < a / 2; i++)
if (s[i] != s[a - i - 1]) {
printf("no");
return 0;
}
printf("yes");
return 0;
}
G - 基础数据结构——栈(1)
给你一串字符,不超过50个字符,可能包括括号、数字、字母、标点符号、空格,你的任务是检查这一串字符中的( ) ,[ ],{ }是否匹配。
Input
输入数据有多组,每组数据不超过100个字符并含有( ,) ,[, ],{, }一个或多个。处理到文件结束。
Output
如果匹配就输出“yes”,不匹配输出“no”
Sample Input
sin(20+10)
{[}]
Sample Output
yes
no
#include <bits/stdc++.h>
using namespace std;
stack<char>sta;
char s[110];
int main() {
while (gets(s)) {
int flag = 0;
for (int i = 0; i < strlen(s); i++) {
if (s[i] == '(' || s[i] == '[' || s[i] == '{') {
sta.push(s[i]);
} else if ((s[i] == ')' && sta.top() == '(') || (s[i] == ']' && sta.top() == '[') || (s[i] == '}'
&& sta.top() == '{')) {
sta.pop();
} else if ((s[i] == ')' && sta.top() != '(') || (s[i] == ']' && sta.top() != '[') || (s[i] == '}'
&& sta.top() != '{')) {
flag = 1;
break;
}
}
if (flag == 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 <bits/stdc++.h>
using namespace std;
char s1[10010], s2[10010];
int main() {
scanf("%s%s", s1,s2);
if (strcmp(s1, s2) < 0)
printf("YES");
else
printf("NO");
return 0;
}
I - 验证子串
输入两个字符串,验证其中一个串是否为另一个串的子串。
输入格式
输入两个字符串, 每个字符串占一行,长度不超过 200200 且不含空格。
输出格式
若第一个串 s_1s1 是第二个串 s_2s2 的子串,则输出"(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<bits/stdc++.h>
using namespace std;
char s1[210], s2[210];
int main() {
scanf("%s%s", s1, s2);
if (strstr(s2, s1))
printf("%s is substring of %s", s1, s2);
else if (strstr(s1, s2))
printf("%s is substring of %s", s2, s1);
else
printf("No substring");
return 0;
}
J - 子串查找
题目描述
这是一道模板题。
给定一个字符串 AA 和一个字符串 BB,求 BB 在 AA 中的出现次数。AA 和 BB 中的字符均为英语大写字母或小写字母。
AA 中不同位置出现的 BB 可重叠。
输入格式
输入共两行,分别是字符串 AA 和字符串 BB。
输出格式
输出一个整数,表示 BB 在 AA 中的出现次数。
样例
Input | Output |
---|---|
zyzyzyz zyz | 3 |
数据范围与提示
1 \leq A, B1≤A,B 的长度 \leq 10 ^ 6≤106,AA、BB 仅包含大小写字母。
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
ull a[1000100], b[1000100];
ull base = 29, t = 0;
char s1[1000100], s2[1000100];
int main() {
b[0] = 1;
scanf("%s%s", s1,s2);
int len1 = strlen(s1), len2 = strlen(s2);
ull ans = 0;
for (int i = 0; i < len1; i++)
a[i + 1] = a[i] * base + (ull)s1[i];
for (int i = 1; i < len1; i++)
b[i] = b[i - 1] * base ;
for (int i = 0; i < len2; i++)
t = t * base + (ull)s2[i];
int cnt = 0;
for (int i = 0; i + len2 <= len1; i++) {
ans = a[i + len2] - a[i] * b[len2];
if (t == ans )
cnt++;
}
printf("%d", cnt);
return 0;
}
K - 剪花布条
题目描述
原题来自:HDU 2087
一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案。对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢?
输入格式
输入数据为多组数据,读取到 #
字符时结束。每组数据仅有一行,为由空格分开的花布条和小饰条。花布条和小饰条都是用可见 ASCII 字符表示的,不会超过 10001000 个字符。
注意:这个 #
应为单个字符。若某字符串开头有 #
,不意味着读入结束!
输出格式
对于每组数据,输出一行一个整数,表示能从花纹布中剪出的最多小饰条个数。
样例
Input | Output |
---|---|
abcde a3 aaaaaa aa # | 0 3 |
数据范围与提示
对于全部数据,字符串长度 \leq 1000≤1000。
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
ull a[10001], b[10001];
ull base = 31;
char s1[10001], s2[10001];
int main() {
b[0] = 1;
while (b[0]) {
ull t = 0;
scanf("%s", s1);
if (strcmp("#", s1) == 0)
return 0;
getchar();
scanf("%s", s2);
int len1 = strlen(s1), len2 = strlen(s2);
ull ans = 0;
for (int i = 0; i < len1; i++)
a[i + 1] = a[i] * base + (ull)s1[i];
for (int i = 1; i < len1; i++)
b[i] = b[i - 1] * base ;
for (int i = 0; i < len2; i++)
t = t * base + (ull)s2[i];
int cnt = 0;
for (int i = 0; i + len2 <= len1; i++) {
ans = a[i + len2] - a[i] * b[len2];
if (t == ans) {
cnt++;
i += len2 - 1;
}
}
printf("%d\n", cnt);
b[0] = 1;
a[0] = 0;
}
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 <bits/stdc++.h>
using namespace std;
const int a = 1e6 + 5;
char s[a * 2], str[a * 2];
int Len[a * 2], len;
int manacher() {
int m = 0, id;
int t = 0;
for (int i = 1; i < len; i++) {
if (m > i)
Len[i] = min(m - i, Len[2 * id - i]);
else
Len[i] = 1;
while (str[i + Len[i]] == str[i - Len[i]])
Len[i]++;
if (Len[i] + i > m) {
m = Len[i] + i;
id = i;
t = max(t, Len[i]);
}
}
return (t - 1);
}
int main() {
scanf("%s", s);
len = strlen(s);
int k = 0;
str[k++] = '@';
for (int i = 0; i < len; i++) {
str[k++] = '#';
str[k++] = s[i];
}
str[k++] = '#';
len = k;
str[k] = 0;
printf("%d", manacher());
return 0;
}