[AOJ]Lesson - ITP1 Topic # 9 String 字符串
作者:互联网
Finding a Word
Write a program which reads a word W and a text T, and prints the number of word W which appears in text T
T consists of string Ti separated by space characters and newlines. Count the number of Ti which equals to W. The word and text are case insensitive.
Constraints
The length of W ≤ 10
W consists of lower case letters
The length of T in a line ≤ 1000
Input
In the first line, the word W is given. In the following lines, the text T is given separated by space characters and newlines.
"END_OF_TEXT" indicates the end of the text.
Output
Print the number of W in the text.
Sample Input
computer
Nurtures computer scientists and highly-skilled computer engineers
who will create and exploit "knowledge" for the new era.
Provides an outstanding computer environment.
END_OF_TEXT
Sample Output
3
题目大意
输入一个单词W,以及一段文本T。问这段文本中,出现过多少次W这个单词。单词W长度不超过10,W保证是小写,文本不区分大小写,文本不超过1000个单词,文本最后以"END_OF_TEXT"结尾。
#include <bits/stdc++.h>
using namespace std;
int main(){
string s1, s2;
cin >> s1;
int ans = 0;
while(cin >> s2){
if(s2=="END_OF_TEXT")
break;
for(int i=0; i<s2.size(); i++) // 转小写
if(s2[i]>='A' && s2[i]<='Z')
s2[i] += 'a'-'A';
if(s1==s2)
ans ++;
}
cout << ans << endl;
return 0;
}
B: Shuffle
Your task is to shuffle a deck of n cards, each of which is marked by a alphabetical letter.
A single shuffle action takes out h cards from the bottom of the deck and moves them to the top of the deck.
The deck of cards is represented by a string as follows.
abcdeefab
The first character and the last character correspond to the card located at the bottom of the deck and the card on the top of the deck respectively.
For example, a shuffle with h = 4 to the above deck, moves the first 4 characters "abcd" to the end of the remaining characters "eefab", and generates the following deck:
eefababcd
You can repeat such shuffle operations.
Write a program which reads a deck (a string) and a sequence of h, and prints the final state (a string).
Input
The input consists of multiple datasets. Each dataset is given in the following format:
A string which represents a deck
The number of shuffle m
h1
h2
.
.
hm
The input ends with a single character '-' for the string.
Constraints
The length of the string ≤ 200
1 ≤ m ≤ 100
1 ≤ hi < The length of the string
The number of datasets ≤ 10
Output
For each dataset, print a string which represents the final state in a line.
Sample Input
aabc
3
1
2
1
vwxyz
2
3
4
Sample Output
aabc
xyzvw
题目大意
输入一个长度不超过200的字符串,经过m次洗牌后,输出最终的字符串。
所谓1次洗牌:洗牌位置是h,即将字符串前h个字符放到原字符串末尾,形成一个新的字符串。
共有若干个字符串,最后以“-”结束。
每个字符串长度不超过200,每个字符串的洗牌操作不超过100,h位置合法,字符串数量不超过10。
#include <bits/stdc++.h>
using namespace std;
int main(){
string s, st;
while(cin >> s){
if(s=="-")
break;
int m, h;
int len = s.size();
cin >> m;
for(int i=1; i<=m; i++){
cin >> h;
s = s.substr(h, len-h) + s.substr(0, h); // 后面 h~len-1的字符串拼接上前面0~h-1的字符串
}
cout << s << endl;
}
return 0;
}
C: Card Game
Taro and Hanako are playing card games. They have n cards each, and they compete n turns. At each turn Taro and Hanako respectively puts out a card. The name of the animal consisting of alphabetical letters is written on each card, and the bigger one in lexicographical order becomes the winner of that turn. The winner obtains 3 points. In the case of a draw, they obtain 1 point each.
Write a program which reads a sequence of cards Taro and Hanako have and reports the final scores of the game.
Input
In the first line, the number of cards n is given. In the following n lines, the cards for n turns are given respectively. For each line, the first string represents the Taro's card and the second one represents Hanako's card.
Constraints
n ≤ 1000
The length of the string ≤ 100
Output
Print the final scores of Taro and Hanako respectively. Put a single space character between them.
Sample Input
3
cat dog
fish fish
lion tiger
Sample Output
1 7
题目大意
Taro 和 Hanako 分别拿出一张卡,比卡中动物的大小,动物用英文书写,规定字典序大的单词胜。胜者得3分,败者不得分,打平双方各得1分。总共进行n论比较,问最终比分是多少。其中n<=1000,单词长度不超过100。
#include <bits/stdc++.h>
using namespace std;
int main(){
string s1, s2;
int n, c1, c2;
cin >> n;
c1 = c2 = 0; //初始化得分
for(int i=1; i<=n; i++){
cin >> s1 >> s2;
if(s1>s2)
c1 += 3;
if(s1==s2)
c1++, c2++;
if(s1<s2)
c2 += 3;
}
cout << c1 << " " << c2 << endl;
return 0;
}
D: Transformation
Write a program which performs a sequence of commands to a given string str. The command is one of:
print a b: print from the a-th character to the b-th character of str
reverse a b: reverse from the a-th character to the b-th character of str
replace a b p: replace from the a-th character to the b-th character of str with p
Note that the indices of str start with 0.
Input
In the first line, a string str is given. str consists of lowercase letters. In the second line, the number of commands q is given. In the next q lines, each command is given in the above mentioned format.
Output
For each print command, print a string in a line.
Constraints
1≤ length of str≤1000
1≤q≤100
0≤a≤b< length of str
for replace command, b−a+1= length of p
Sample Input 1
abcde
3
replace 1 3 xyz
reverse 0 2
print 1 4
Sample Output 1
xaze
Sample Input 2
xyz
3
print 0 2
replace 0 2 abc
print 0 2
Sample Output 2
xyz
abc
题目大意
给定一个字符串str,可以对字符串进行3中操作(第一个位置从0开始算起):对区间a~b打印、区间a~b进行反转、区间a~b被字符串p替换。总共有q次操作,1<=q<=100。字符串操作均合法。
#include <bits/stdc++.h>
using namespace std;
int main(){
string op, s, p;
int q, a, b;
cin >> s;
cin >> q;
for(int i=1; i<=q; i++){
cin >> op >> a >> b;
if(op=="replace"){
cin >> p;
s.replace(a, b-a+1, p); // replace(起始位置,长度,替换字符串)
}
if(op=="reverse"){
for(int j=a; j<=(a+b)/2; j++) // 反转
swap(s[j], s[a+b-j]);
}
if(op=="print"){
for(int j=a; j<=b; j++)
cout << s[j];
cout << endl;
}
}
return 0;
}
标签:ITP1,String,deck,int,character,AOJ,str,字符串,string 来源: https://www.cnblogs.com/gdgzliu/p/11891048.html