当前位置:网站首页>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;
}
边栏推荐
- Toolbutton property settings
- Label Semantic Aware Pre-training for Few-shot Text Classification
- Connect Alibaba cloud servers in the form of key pairs
- Simulate mouse click
- 20220601 Mathematics: zero after factorial
- Leetcode 300 最长上升子序列
- When the reference is assigned to auto
- 使用sed替换文件夹下文件
- Leetcode 300 longest ascending subsequence
- 3.3 Monte Carlo Methods: case study: Blackjack of Policy Improvement of on- & off-policy Evaluation
猜你喜欢

Leetcode - 1670 design front, middle and rear queues (Design - two double ended queues)

LeetCode - 706 设计哈希映射(设计) *

Leetcode bit operation

Opencv+dlib to change the face of Mona Lisa

Flutter 退出当前操作二次确认怎么做才更优雅?

Configure opencv in QT Creator
![[combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)](/img/19/5dc152b3fadeb56de50768561ad659.jpg)
[combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)

Retinaface: single stage dense face localization in the wild

yocto 技術分享第四期:自定義增加軟件包支持

Opencv histogram equalization
随机推荐
CV learning notes - image filter
QT self drawing button with bubbles
使用sed替换文件夹下文件
Leetcode - 706 design hash mapping (Design)*
CV learning notes - clustering
Leetcode - 460 LFU cache (Design - hash table + bidirectional linked hash table + balanced binary tree (TreeSet))*
LeetCode - 715. Range 模块(TreeSet) *****
Tensorflow2.0 save model
Boston house price forecast (tensorflow2.9 practice)
Notes - regular expressions
使用密钥对的形式连接阿里云服务器
Dictionary tree prefix tree trie
Opencv feature extraction - hog
LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)
【C 题集】of Ⅵ
20220610 other: Task Scheduler
20220604数学:x的平方根
Opencv gray histogram, histogram specification
20220603 Mathematics: pow (x, n)
Leetcode-404: sum of left leaves