HDOJ 字符串类型

文章目录
  1. 1. 1020 Encoding
  2. 2. 1039 Easier Done Than Said?
  3. 3. 1062 Text Reverse
  4. 4. 1073 Online Judge
  5. 5. 1075 What Are You Talking About
  6. 6. 1088 Write a simple HTML Browser
  7. 7. 1113 Word Amalgamation
  8. 8. 1161 Eddy’s mistakes
  9. 9. 1200 To and Fro
  10. 10. 1251 统计难题
  11. 11. 1256 画8
  12. 12. 1321 Reverse Text
  13. 13. 1328 IBM Minus One

HDOJ刷题之字符串类型题目合集~

1020 Encoding

http://acm.hdu.edu.cn/showproblem.php?pid=1020

这个是一个简单的问题,但是我以为是统计所有字母的总数,没想到是按段数计算,所以下面的方法错误,wa…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
while(n--)
{
string str;
cin>>str;
map<char,int>cnt;
for(int i=0;i<str.size();i++)
cnt[str[i]]+=1;
for(map<char,int>::iterator i=cnt.begin();i!=cnt.end();i++)
{
if(i->second!=1) cout<<i->second<<i->first;
else cout<<i->first;
}
cout<<endl;
}

}
return 0;
}

这个就是AC代码…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
while(n--)
{
string str;
cin>>str;
int sum=1;
for(int i=0;i<str.size();i++)
{

if(str[i]!=str[i+1])
{
if(sum==1) cout<<str[i];
else cout<<sum<<str[i];
sum=1;
}
else sum+=1;
}
cout<<endl;
}

}
return 0;
}

1039 Easier Done Than Said?

http://acm.hdu.edu.cn/showproblem.php?pid=1039

思考了一会,一次AC啊哈哈哈哈哈~

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <bits/stdc++.h>
using namespace std;

bool checkV(string str)
{
for(int i=0;i<str.size();i++)
{
if(str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' || str[i]=='u')
return true;
}
return false;
}

int main()
{
string str;
while(cin>>str&&str!="end")
{
bool flag=false;
if(!checkV(str)) cout<<"<"<<str<<"> "<<"is not acceptable."<<endl;
else
{
int cntv=0,cntf=0,cnt=1;
for(int i=0;i<str.size();i++)
{
if(str[i]==str[i+1])
{
cnt+=1;
if(cnt==2)
{
if(str[i]!='e'&&str[i]!='o')
{
flag=true;
//cout<<str[i]<<endl;
cout<<"<"<<str<<"> "<<"is not acceptable."<<endl;
break;
}
}
}
else cnt=1;
if(str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' || str[i]=='u')
{
cntv+=1;
if(cntv==3)
{
flag=true;
cout<<"<"<<str<<"> "<<"is not acceptable."<<endl;
break;
}
}
else cntv=0;
if(!(str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' || str[i]=='u'))
{
cntf+=1;
if(cntf==3)
{
flag=true;
cout<<"<"<<str<<"> "<<"is not acceptable."<<endl;
break;
}
}
else cntf=0;
}
if(!flag) cout<<"<"<<str<<"> "<<"is acceptable."<<endl;
}
}
return 0;
}

1062 Text Reverse

http://acm.hdu.edu.cn/showproblem.php?pid=1062

PE到人生绝望…这道题关键在于有多少个空格都要包含进去…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
getchar();
for(int i=0;i<n;i++)
{
string str;
string ans;
getline(cin,str);
int cnt=0;
for(int i=0;i<str.size();i++)
{
if(str[i]==' ')
{
cnt+=1;
}
if(str[i]==' '&&str[i+1]!=' ')
{
for(int i=ans.size()-1;i>=0;i--)
cout<<ans[i];
ans="";
for(int i=0;i<cnt;i++)
cout<<" ";
cnt=0;
}
if(str[i]!=' ') ans+=str[i];
if(i==str.size()-1)
{
for(int i=ans.size()-1;i>=0;i--)
cout<<ans[i];
}
}
cout<<endl;
}
}
return 0;
}

1073 Online Judge

这道题最关键的地方在于输入,尤其是在输入完的字符串后面加上的’\n’
http://acm.hdu.edu.cn/showproblem.php?pid=1073

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
while(n--)
{
string s;
string str1,str2;
while(getline(cin,s)&&s!="START");
while(getline(cin,s))
{
if(s=="END") break;
str1+=s;
str1+='\n';
}
while(getline(cin,s)&&s!="START");
while(getline(cin,s))
{
if(s=="END") break;
str2+=s;
str2+='\n';
}
string s1,s2;
for(int i=0;i<str1.size();i++)
{
if(str1[i]!=' '&&str1[i]!='\t'&&str1[i]!='\n') s1+=str1[i];
}
for(int i=0;i<str2.size();i++)
{
if(str2[i]!=' '&&str2[i]!='\t'&&str2[i]!='\n') s2+=str2[i];
}
if(str1==str2) cout<<"Accepted"<<endl;
else
{
if(s1==s2) cout<<"Presentation Error"<<endl;
else cout<<"Wrong Answer"<<endl;
}
}
}
return 0;
}

1075 What Are You Talking About

http://acm.hdu.edu.cn/showproblem.php?pid=1075
下面这个是AC代码,这道题有一个坑点,就是如果使用getline(cin,str)会超时,必须使用scanf…
还有就是gets可以读入包含空格的char字段。
map下面提供的查找find函数就是查标号,查到对应的标号然后就可以取值,比我之前的暴力循环要强。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <bits/stdc++.h>
using namespace std;
int main()
{
char s[3005];
scanf("%*s");
map<string,string>dic;
while(scanf("%s",s)!=EOF&&strcmp(s,"END"))
{
char tmp[3005];
scanf("%s",tmp);
dic[tmp]=s;
}
scanf("%*s%*c");
while(gets(s)&&strcmp(s,"END"))
{
vector<string>vec;
string tmp="";
string mark;
map<string, string>::iterator it;
for(int i=0; i<strlen(s); i++)
{
if(s[i]>='a'&&s[i]<='z') tmp+=s[i];
else
{
tmp+="\0";
it=dic.find(tmp);
if(it!=dic.end()) cout<<it->second;
else cout<<tmp;
tmp="";
}
if(tmp=="") cout<<s[i];

}
cout<<endl;
}
return 0;
}

下面放一下我的花式超时代码23333,以此警戒

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s;
while(getline(cin,s)&&s!="START");
map<string,string>dic;
while(getline(cin,s))
{
if(s=="END")
break;
string tmp;
string str;
bool flag=false;
for(int i=0; i<s.size(); i++)
{
if(s[i]!=' '&&!flag)
tmp+=s[i];
else
flag=true;
if(flag&&s[i]!=' ')
str+=s[i];
}
dic[tmp]=str;
}
/*
map<string,string>::iterator iter;
for(iter=dic.begin();iter!=dic.end();iter++)
{
cout<<iter->first<<":"<<iter->second<<endl;
}*/
while(getline(cin,s)&&s!="START");
while(getline(cin,s))
{
if(s=="END")
break;
vector<string>vec;
string tmp="";
string mark;
for(int i=0; i<s.size(); i++)
{
if(s[i]!=' '&&s[i]!=','&&s[i]!='!'&&s[i]!='\n'&&s[i]!='\t'&&s[i]!='.')
{
tmp+=s[i];
}
else
{
mark=s[i];
vec.push_back(tmp);
vec.push_back(mark);
tmp="";
}
}
map<string,string>::iterator iter;
/*
for(int i=0;i<vec.size();i++)
cout<<vec[i];
cout<<endl;*/
for(int i=0; i<vec.size(); i++)
{
bool flag=false;
for(iter=dic.begin(); iter!=dic.end(); iter++)
{
if(vec[i]==iter->second)
{
flag=true;
cout<<iter->first;
break;
}
}
if(!flag) cout<<vec[i];
}
cout<<endl;
}
return 0;
}

1088 Write a simple HTML Browser

http://acm.hdu.edu.cn/showproblem.php?pid=1088

疯狂PE,不改了不改了,就这样吧…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <bits/stdc++.h>
using namespace std;

void hr()
{
for(int i=1;i<=80;i++)
cout<<"-";
}

int main()
{
string str;
int pos=0;
while(cin>>str)
{
if(str=="<br>")
{
pos=0;
cout<<endl;
}
else if(str=="<hr>")
{
if(pos) cout<<endl;
hr();
pos=0;
}
else
{
pos+=str.size();
if(pos<80)
{
cout<<str;
if(pos)
{
pos+=1;
cout<<" ";
}
}
else
{
cout<<endl;
cout<<str;
pos=0;
pos+=str.size();
}
}
cout<<endl;
}
return 0;
}

1113 Word Amalgamation

http://acm.hdu.edu.cn/showproblem.php?pid=1113

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <bits/stdc++.h>
using namespace std;
map<string,string> m;
string s,str;
int main()
{
while(cin>>s)
{
if(s=="XXXXXX")
break;
str=s;
sort(s.begin(),s.end());
m[str]=s;
}
while(cin>>s)
{
if(s=="XXXXXX")
break;
sort(s.begin(),s.end());
map<string,string>::iterator it;
int flag=0;
for(it=m.begin();it!=m.end();it++)
{
if(it->second==s)
{
flag=1;
cout<<it->first<<endl;
}
}
if(!flag)
cout<<"NOT A VALID WORD"<<endl;
cout<<"******"<<endl;
}
}

1161 Eddy’s mistakes

http://acm.hdu.edu.cn/showproblem.php?pid=1161

这题安慰了我被伤害的小心心…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str;
while(getline(cin,str))
{
for(int i=0;i<str.size();i++)
{
if(str[i]>='A'&&str[i]<='Z')
cout<<((char)(str[i]-'A'+'a'));
else cout<<str[i];
}
cout<<endl;
}
return 0;
}

1200 To and Fro

http://acm.hdu.edu.cn/showproblem.php?pid=1200

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <bits/stdc++.h>
using namespace std;
#define maxn 205
char maps[maxn][maxn];
int main()
{
int n;
while(cin>>n&&n)
{
string str;
cin>>str;
int cnt1=0,cnt2=0;
bool flag=true;
while(cnt2<str.size())
{
if(!flag)
{
for(int i=n-1;i>=0;i--)
{
maps[cnt1][i]=str[cnt2];
cnt2+=1;
}
flag=true;
}
else if(flag)
{
for(int i=0; i<n; i++)
{
maps[cnt1][i]=str[cnt2];
cnt2+=1;
}
flag=false;
}
cnt1+=1;
}
for(int i=0;i<n;i++)
for(int j=0;j<cnt1;j++)
cout<<maps[j][i];
cout<<endl;
}
return 0;
}

1251 统计难题

http://acm.hdu.edu.cn/showproblem.php?pid=1251

这题用vector遍历会超时,所以在存字符串的时候直接把单独的字符存入,便于后面直接调用,而且不会超时。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <bits/stdc++.h>
using namespace std;
int main()
{
map<string,int>m;
string str;
char c;
while(1)
{
scanf("%c",&c);
if(c=='\n')
{
str="";
scanf("%c",&c);
}
if(c=='\n')
break;
str+=c;
m[str]+=1;
}
while(cin>>str)
{
cout<<m[str]<<endl;
}
return 0;
}

1256 画8

http://acm.hdu.edu.cn/showproblem.php?pid=1256
疯狂PE,我不改了,就这样吧,哼!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <bits/stdc++.h>
using namespace std;

void printRaw(char c,int cnt,int k)
{
for(int i=0; i<k; i++)
cout<<" ";
for(int i=0; i<cnt; i++)
{
cout<<c;
}
cout<<endl;
}

void printCol(char c,int cnt,int k)
{
for(int i=0; i<k; i++)
cout<<c;
for(int i=0; i<cnt; i++)
cout<<" ";
for(int i=0; i<k; i++)
cout<<c;
cout<<endl;
}

int main()
{
int n;
cin>>n;
while(n--)
{
int n;
char c;
cin>>c>>n;
int k=n/6+1;
int tmp=(n-3)/2;
int cnt=n-3-tmp;
printRaw(c,cnt,k);
for(int i=0; i<tmp; i++)
printCol(c,cnt,k);
printRaw(c,cnt,k);
for(int i=tmp; i<n-3; i++)
printCol(c,cnt,k);
printRaw(c,cnt,k);
//cout<<endl;
}
return 0;
}

1321 Reverse Text

http://acm.hdu.edu.cn/showproblem.php?pid=1321

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
getchar();
while(n--)
{
string str;
getline(cin,str);
for(int i=str.size()-1;i>=0;i--)
cout<<str[i];
cout<<endl;
}
}
return 0;
}

1328 IBM Minus One

http://acm.hdu.edu.cn/showproblem.php?pid=1328

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
for(int j=1;j<=n;j++)
{
char str[1005];
cin>>str;
cout<<"String #"<<j<<endl;
for(int i=0;i<strlen(str);i++)
{
if(str[i]=='Z') cout<<'A';
else cout<<(char)(str[i]+1);
}
cout<<endl<<endl;
}
}
return 0;
}