洛谷试炼场2-5---字符串处理【字符串】
P1603 斯诺登密码
https://www.luogu.org/problemnew/show/P1603
思路:题目里写的非正规是真的坑啊,我还以为是非正规输入输入这些是不能算数字的,其实是要算的,而且 another 是什么鬼居然算1.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <set> 3 #include <cmath> 4 #include <stdio.h> 5 #include <cstring> 6 #include <algorithm> 7 #include <vector> 8 #include <queue> 9 #include <map> 10 using namespace std; 11 typedef long long LL; 12 #define inf 0x7f7f7f7f 13 14 string number[21] = {"", "one", "two", "three", "four", "five", "six", "seven", 15 "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", 16 "sixteen", "seventeen", "eighteen", "nineteen", "twenty"}; 17 string number1[3] = {"", "a", "both"}; 18 string number2[4] = {"", "first", "second", "third"}; 19 20 bool cmp(int i, int j) 21 { 22 if(i / 10 == j / 10) return (i % 10) < (j % 10); 23 else return (i / 10) < (j / 10); 24 } 25 26 int main() 27 { 28 string s; 29 int nums[6]; 30 int cnt = 0; 31 for(int i = 0; i < 6; i++){ 32 cin>>s; 33 for(int j = 0; j < 21; j++){ 34 if(s == number[j]){ 35 //cout<<number[j]<<" "<<s<<endl; 36 nums[cnt++] = j * j % 100; 37 break; 38 } 39 } 40 for(int j = 0; j < 3; j++){ 41 if(s == number1[j]){ 42 nums[cnt++] = j * j % 100; 43 break; 44 } 45 } 46 if(s == "another"){ 47 nums[cnt++] = 1; 48 } 49 for(int j = 0; j < 4; j++){ 50 if(s == number2[j]){ 51 nums[cnt++] = j * j % 100; 52 break; 53 } 54 } 55 } 56 57 sort(nums, nums + cnt, cmp); 58 printf("%d", nums[0]); 59 for(int i = 1; i < cnt; i++){ 60 if(nums[i] < 10){ 61 printf("0"); 62 } 63 printf("%d", nums[i]); 64 } 65 printf("\n"); 66 return 0; 67 }
P1071 潜伏者
https://www.luogu.org/problemnew/show/P1071
思路:要求的是一个字母对应的是一个密文,一个密文对应的是一个字母,要用两个map。而且要求每个字母都有出现。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <set> 3 #include <cmath> 4 #include <stdio.h> 5 #include <cstring> 6 #include <algorithm> 7 #include <vector> 8 #include <queue> 9 #include <map> 10 using namespace std; 11 typedef long long LL; 12 #define inf 0x7f7f7f7f 13 14 char sec[105], str[105]; 15 map<char, char>mp, revmp; 16 17 int main() 18 { 19 scanf("%s", sec); 20 scanf("%s", str); 21 int len = strlen(sec); 22 for(int i = 0; i < len; i++){ 23 if(mp.find(sec[i]) == mp.end()){ 24 mp[sec[i]] = str[i]; 25 } 26 else if(mp[sec[i]] != str[i]){ 27 printf("Failed\n"); 28 return 0; 29 } 30 31 if(revmp.find(str[i]) == revmp.end()){ 32 revmp[str[i]] = sec[i]; 33 } 34 else if(revmp[str[i]] != sec[i]){ 35 printf("Failed\n"); 36 return 0; 37 } 38 } 39 for(int i = 0; i < 26; i++){ 40 if(mp.find('A' + i) == mp.end()){ 41 printf("Failed\n"); 42 return 0; 43 } 44 } 45 scanf("%s", sec); 46 len = strlen(sec); 47 for(int i = 0; i < len; i++){ 48 if(mp.find(sec[i]) == mp.end()){ 49 printf("Failed"); 50 } 51 else{ 52 printf("%c", mp[sec[i]]); 53 } 54 } 55 printf("\n"); 56 return 0; 57 }
P1012 拼数
https://www.luogu.org/problemnew/show/P1012
思路:把输入的整数当成是string,对他们排序,大的放前面。有一个需要注意的点,比如两个数30,300.如果只是单纯比较字符串的大小,会得到30030,但显然答案应该是30300.因此不能单纯比较字符串的大小,而是比较a+b>b+a,意思是两个字符串拼接后较大的放前面。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <set> 3 #include <cmath> 4 #include <stdio.h> 5 #include <cstring> 6 #include <algorithm> 7 #include <vector> 8 #include <queue> 9 #include <map> 10 using namespace std; 11 typedef long long LL; 12 #define inf 0x7f7f7f7f 13 14 string num[21]; 15 int n; 16 17 bool cmp(string a, string b) 18 { 19 /*if(a.length() == b.length()){ 20 return a > b; 21 } 22 else return a.length() > b.length();*/ 23 return a + b > b + a; 24 } 25 26 int main() 27 { 28 scanf("%d", &n); 29 for(int i = 0; i < n; i++){ 30 cin>>num[i]; 31 } 32 sort(num, num + n, cmp); 33 for(int i = 0; i < n; i++){ 34 cout<<num[i]; 35 } 36 cout<<endl; 37 return 0; 38 }
P1538 迎春舞会之数字舞蹈【毒瘤】
https://www.luogu.org/problemnew/show/P1538
思路:暴力模拟输出。本来以为还需要复杂一点考虑1啊宽度和别的数不一样,然后就拿了个0分。不用考虑的话写起来还简单很多。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <set> 3 #include <cmath> 4 #include <stdio.h> 5 #include <cstring> 6 #include <algorithm> 7 #include <vector> 8 #include <queue> 9 #include <map> 10 using namespace std; 11 typedef long long LL; 12 #define inf 0x7f7f7f7f 13 14 int k; 15 char s[300]; 16 17 18 19 int main() 20 { 21 //freopen("out.txt", "w", stdout); 22 scanf("%d", &k); 23 scanf("%s", s); 24 int n = strlen(s); 25 26 for(int i = 0; i < n; i++){ 27 if(i != 0)printf(" "); 28 if(s[i] == '1' || s[i] == '4'){ 29 for(int j = 0; j < k + 2; j++){ 30 printf(" "); 31 } 32 } 33 else{ 34 printf(" "); 35 for(int j = 0; j < k; j++){ 36 printf("-"); 37 } 38 printf(" "); 39 } 40 } 41 printf("\n"); 42 43 for(int hang = 0; hang < k; hang++){ 44 for(int i = 0; i < n; i++){ 45 if(i != 0)printf(" "); 46 if(s[i] == '1' || s[i] == '2' || s[i] == '3' || s[i] == '7'){ 47 for(int j = 0; j < k + 1; j++){ 48 printf(" "); 49 } 50 printf("|"); 51 } 52 else if(s[i] == '5' || s[i] == '6'){ 53 printf("|"); 54 for(int j = 0; j < k + 1; j++){ 55 printf(" "); 56 } 57 } 58 else{ 59 printf("|"); 60 for(int j = 0; j < k; j++){ 61 printf(" "); 62 } 63 printf("|"); 64 } 65 } 66 printf("\n"); 67 } 68 69 for(int i = 0; i < n; i++){ 70 if(i != 0)printf(" "); 71 if(s[i] == '1' || s[i] == '7' || s[i] == '0'){ 72 for(int j = 0; j < k + 2; j++){ 73 printf(" "); 74 } 75 } 76 else{ 77 printf(" "); 78 for(int j = 0; j < k; j++){ 79 printf("-"); 80 } 81 printf(" "); 82 } 83 } 84 printf("\n"); 85 86 for(int hang = 0; hang < k; hang++){ 87 for(int i = 0; i < n; i++){ 88 if(i != 0)printf(" "); 89 if(s[i] == '1' || s[i] == '3' || s[i] == '7' || s[i] == '4' || s[i] =='5' || s[i] == '9'){ 90 for(int j = 0; j < k + 1; j++){ 91 printf(" "); 92 } 93 printf("|"); 94 } 95 else if(s[i] == '2'){ 96 printf("|"); 97 for(int j = 0; j < k + 1; j++){ 98 printf(" "); 99 } 100 } 101 else{ 102 printf("|"); 103 for(int j = 0; j < k; j++){ 104 printf(" "); 105 } 106 printf("|"); 107 } 108 } 109 printf("\n"); 110 } 111 112 for(int i = 0; i < n; i++){ 113 if(i != 0)printf(" "); 114 if(s[i] == '1' || s[i] == '4' || s[i] == '7'){ 115 for(int j = 0; j < k + 2; j++){ 116 printf(" "); 117 } 118 } 119 else{ 120 printf(" "); 121 for(int j = 0; j < k; j++){ 122 printf("-"); 123 } 124 printf(" "); 125 } 126 } 127 printf("\n"); 128 129 return 0; 130 }