当前位置:网站首页>Codeup: word replacement
Codeup: word replacement
2022-07-03 10:19:00 【Innocent^_^】
Title address :http://codeup.hustoj.com/problem.php?cid=100000580&pid=3
(codeup Changed places ,《 Algorithm notes 》 The above is invalid )
Specific ideas can be seen in the notes , Here is an important question :
getline(cin,s) Sometimes you will assign empty input to s, To avoid this problem, just write :
while(getline(cin,sentence)){
// prevent geiline Convulsion
if(sentence.empty())continue;
Here's what you can do ac Code for :
#include<iostream>
using namespace std;
bool findWord(char words[][101],string s,int r){
// Compare whether the word is s
int s_len=s.length();
int word_len=0;
int count=s_len;//count Reduced to 0 explain s After comparison
for(int i=0;i<s_len;++i){
// There is a position character that is not aligned and returns false
if(words[r][i]=='\0' || words[r][i]!=s[i])return false;
else --count;
}
// cout<<count<<endl;
// The string to be replaced has not been deducted or the word has not been compared , return false
if(count>0 || words[r][s_len]!='\0')return false;
return true;
}
void replaceWord(char words[][101],string s,int r){
int len=s.length();
for(int i=0;i<len;++i){
words[r][i]=s[i];
}
words[r][len]='\0';
}
int main(){
string sentence,beReplaced,newword;
while(getline(cin,sentence)){
// prevent geiline Convulsion
if(sentence.empty())continue;
cin>>beReplaced>>newword;
// Initialize the word storage two-dimensional array as all terminators .r and c Corresponding rows and columns
char words[101][101]={
'\0'};int r=0,c=0;
int sentence_len=sentence.length();
// Store words
for(int i=0;i<sentence_len;++i){
if(sentence[i]!=' '){
words[r][c++]=sentence[i];
}
else{
// Move to the next line ,c Zeroing
words[r++][c]='\0';
c=0;
}
}
// Start changing words
for(int i=0;i<=r;++i){
if(findWord(words,beReplaced,i)){
// Meet the replacement conditions
replaceWord(words,newword,i);
}
}
// Output
for(int i=0;i<=r;++i){
if(words[r][0]=='\0')break;
for(int j=0;words[i][j]!='\0';++j)
cout<<words[i][j];
cout<<' ';
}
// Reset three string variables
sentence.clear();
cout<<'\n';
}
return 0;
}
边栏推荐
- Simulate mouse click
- 使用sed替换文件夹下文件
- 2021-11-11 standard thread library
- Retinaface: single stage dense face localization in the wild
- LeetCode - 900. RLE iterator
- CV learning notes - image filter
- Tensorflow built-in evaluation
- MySQL root user needs sudo login
- Leetcode - 460 LFU cache (Design - hash table + bidirectional linked hash table + balanced binary tree (TreeSet))*
- [LZY learning notes -dive into deep learning] math preparation 2.5-2.7
猜你喜欢
LeetCode - 1172 餐盘栈 (设计 - List + 小顶堆 + 栈))
openCV+dlib实现给蒙娜丽莎换脸
Opencv feature extraction - hog
波士顿房价预测(TensorFlow2.9实践)
MySQL root user needs sudo login
Opencv image rotation
Vgg16 migration learning source code
LeetCode - 895 最大频率栈(设计- 哈希表+优先队列 哈希表 + 栈) *
CV learning notes - image filter
Leetcode-513:找树的左下角值
随机推荐
20220607 others: sum of two integers
CV learning notes ransca & image similarity comparison hash
Deep learning by Pytorch
After clicking the Save button, you can only click it once
Leetcode 300 最长上升子序列
openCV+dlib实现给蒙娜丽莎换脸
Leetcode - 933 number of recent requests
Leetcode - 706 design hash mapping (Design)*
LeetCode - 703 数据流中的第 K 大元素(设计 - 优先队列)
CV learning notes - feature extraction
QT is a method of batch modifying the style of a certain type of control after naming the control
Discrete-event system
LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)
Step 1: teach you to trace the IP address of [phishing email]
Leetcode-112: path sum
20220610其他:任务调度器
Leetcode-106:根据中后序遍历序列构造二叉树
2021-11-11 standard thread library
Rewrite Boston house price forecast task (using paddlepaddlepaddle)
Leetcode bit operation