小写字母比大写字母ascll码多32
做法1.但是TLE
https://www.luogu.com.cn/problem/P1308
#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <sstream>
using namespace std;
typedef long long ll;
int main()
{
string str1 , str2 , empty_str;
char ch;
int pos = 0 , cnt = 0;
cin >> str1;
getchar();
for(char &s: str1) if(s<='Z'&&s>='A') s += 32;
while((ch = getchar())!='\n'){
if(ch!=' '){
cin >> str2;
str2 = ch + str2;
for(char &s: str2) if(s<='Z'&&s>='A') s += 32;
if(str2==str1){
cnt++;
}
else{
if(cnt==0)
pos += str2.length();
}
str2 = empty_str;
}
else{
if(cnt==0)
pos ++;
}
}
cout << str2 <<endl;
if(cnt==0) cout << -1;
else cout << cnt<<' '<<pos;
return 0;
}
做法2.还是超时 但是少了一些 但是内存占的巨大
#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <sstream>
using namespace std;
typedef long long ll;
int main()
{
string str1;
cin >> str1;
for(char &a : str1) if(a<='Z'&&a>='A') a += 32;
int pos = 0,_time = 0;
string str2,empty_str2;
getchar();
char ch;
while(1){
ch = getchar();
if(ch=='\n'){
if(str2==str1){
_time++;
}
// else{
// if(_time==0) pos += str2.length();
// }
break;
}
else if(ch==' '){
if(str2==str1){
_time ++;
}
if(_time==0){
pos+= 1 + str2.length();
}
str2 = empty_str2;
}
else{
str2 += ch;
}
}
//cout << str2;
if(_time==0) cout <<-1;
else cout <<_time<<' '<<pos;
return 0;
}