|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include <bits/stdc++.h>
- using namespace std;
- void low(string &s){
- for(char i : s){
- i = tolower(i);
- }
- }
- string tar, temp = "***"; //tar是目标, temp临时变量
- int tarsize, c = 0, l = 0; //第一个是目标的长度, c代表找到几次, l是实时更新的位置
- vector<int> locate; //记录位置
- bool isfind = false;
- int main(){
- ios::sync_with_stdio(false);
- cin >> tar;
- low(tar);
- tarsize = tar.size();
- while(temp != "\0"){
- cin >> temp;
- low(temp);
- if(tar == temp){
- isfind = true;
- c++;
- locate.push_back(l);
- }
- l += temp.size() + 1; //加一有空格
- }
- if(isfind){
- cout << locate[0] << ' ' << c;
- }
- else{
- cout << "-1";
- }
- return 0;
- }
复制代码
代码如上
题目:https://www.luogu.com.cn/problem/P1308
这次我的想法是反正每个单词之间都有空格, 可以用cin一个一个读, 然后再做出返回
但是现在运行起来它不返回 , 不知道为啥...
求助
- #include <bits/stdc++.h>
- using namespace std;
- void low(string &s) {
- for(char &i : s) {
- i = tolower(i);
- }
- }
- string tar, temp = "***"; // tar是目标, temp临时变量
- int tarsize, c = 0, l = 0; //第一个是目标的长度, c代表找到几次, l是实时更新的位置
- vector<int> locate; //记录位置
- bool isfind = false;
- int main() {
- ios::sync_with_stdio(false);
- cin >> tar;
- low(tar);
- tarsize = tar.size();
- while(temp != "\0") {
- cin >> temp;
- if(!cin.good()) temp = "\0";
- low(temp);
- if(tar == temp) {
- isfind = true;
- c++;
- locate.push_back(l);
- }
- l += temp.size() + 1; //加一有空格
- }
- if(isfind) {
- cout << locate[0] << ' ' << c;
- } else {
- cout << "-1";
- }
- cout << endl;
- return 0;
- }
复制代码
|
|