鱼C论坛

 找回密码
 立即注册
查看: 1521|回复: 3

[已解决]匹配字符串(这次是不同方法)

[复制链接]
发表于 2022-4-28 23:07:38 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include <bits/stdc++.h>
  2. using namespace std;

  3. void low(string &s){
  4.         for(char i : s){
  5.                 i = tolower(i);
  6.         }
  7. }

  8. string tar, temp = "***"; //tar是目标, temp临时变量
  9. int tarsize, c = 0, l = 0; //第一个是目标的长度, c代表找到几次, l是实时更新的位置
  10. vector<int> locate; //记录位置
  11. bool isfind = false;
  12. int main(){
  13.         ios::sync_with_stdio(false);
  14.         cin >> tar;
  15.         low(tar);
  16.         tarsize = tar.size();
  17.         while(temp != "\0"){
  18.                 cin >> temp;
  19.                 low(temp);
  20.                 if(tar == temp){
  21.                         isfind = true;
  22.                         c++;
  23.                         locate.push_back(l);
  24.                 }
  25.                 l += temp.size() + 1; //加一有空格
  26.         }
  27.         if(isfind){
  28.                 cout << locate[0] << ' ' << c;
  29.         }
  30.         else{
  31.                 cout << "-1";
  32.         }
  33.         return 0;
  34. }
复制代码

代码如上
题目:https://www.luogu.com.cn/problem/P1308
这次我的想法是反正每个单词之间都有空格, 可以用cin一个一个读, 然后再做出返回
但是现在运行起来它不返回 , 不知道为啥...
求助
最佳答案
2022-4-29 17:10:04
  1. #include <bits/stdc++.h>

  2. using namespace std;

  3. void low(string &s) {
  4.     for(char &i : s) {
  5.         i = tolower(i);
  6.     }
  7. }

  8. string tar, temp = "***";   // tar是目标, temp临时变量
  9. int tarsize, c = 0, l = 0;  //第一个是目标的长度, c代表找到几次, l是实时更新的位置
  10. vector<int> locate; //记录位置
  11. bool isfind = false;

  12. int main() {
  13.     ios::sync_with_stdio(false);
  14.     cin >> tar;
  15.     low(tar);
  16.     tarsize = tar.size();
  17.     while(temp != "\0") {
  18.         cin >> temp;
  19.         if(!cin.good()) temp = "\0";
  20.         low(temp);
  21.         if(tar == temp) {
  22.             isfind = true;
  23.             c++;
  24.             locate.push_back(l);
  25.         }
  26.         l += temp.size() + 1; //加一有空格
  27.     }
  28.     if(isfind) {
  29.         cout << locate[0] << ' ' << c;
  30.     } else {
  31.         cout << "-1";
  32.     }
  33.     cout << endl;
  34.     return 0;
  35. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-4-28 23:21:11 | 显示全部楼层

  1. void low(string &s){
  2.         for(char i : s){
  3.                 i = tolower(i);
  4.         }
  5. }
复制代码



for(char &i : s)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-4-29 15:02:42 | 显示全部楼层


还是没有输出qwq
谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-4-29 17:10:04 | 显示全部楼层    本楼为最佳答案   
  1. #include <bits/stdc++.h>

  2. using namespace std;

  3. void low(string &s) {
  4.     for(char &i : s) {
  5.         i = tolower(i);
  6.     }
  7. }

  8. string tar, temp = "***";   // tar是目标, temp临时变量
  9. int tarsize, c = 0, l = 0;  //第一个是目标的长度, c代表找到几次, l是实时更新的位置
  10. vector<int> locate; //记录位置
  11. bool isfind = false;

  12. int main() {
  13.     ios::sync_with_stdio(false);
  14.     cin >> tar;
  15.     low(tar);
  16.     tarsize = tar.size();
  17.     while(temp != "\0") {
  18.         cin >> temp;
  19.         if(!cin.good()) temp = "\0";
  20.         low(temp);
  21.         if(tar == temp) {
  22.             isfind = true;
  23.             c++;
  24.             locate.push_back(l);
  25.         }
  26.         l += temp.size() + 1; //加一有空格
  27.     }
  28.     if(isfind) {
  29.         cout << locate[0] << ' ' << c;
  30.     } else {
  31.         cout << "-1";
  32.     }
  33.     cout << endl;
  34.     return 0;
  35. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-24 11:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表