uva 156 Ananagrams(字符串模拟搜索)
2014-07-02 00:22
429 查看
题意是找到一个字符串里面所有可以重排的单词,最后字典序输出。
把所有单词找出来,全换成小写,并将字母排序。
搜索排序后一样的单词,标记。
最后没上标记的字典序输出就行了。
脑袋糊糊的,写的乱七八糟的。
代码:
把所有单词找出来,全换成小写,并将字母排序。
搜索排序后一样的单词,标记。
最后没上标记的字典序输出就行了。
脑袋糊糊的,写的乱七八糟的。
代码:
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; const int Maxn = 1010; struct String { char str[100]; char low[100]; bool no;//记录单词是否存在 } s[Maxn]; struct Ans//保存结果 { char str[100]; } a[Maxn]; //字典序排序 bool cmp(Ans a, Ans b) { return strcmp(a.str, b.str) < 0; } int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); #endif // LOCAL int cnt = 0; while (scanf("%s", s[cnt].str) != EOF) { if (s[cnt].str[0] == '#') break; //将大写转换成小写 for (int i = 0; i < strlen(s[cnt].str); i++) { if (s[cnt].str[i] <= 'Z' && s[cnt].str[i] >= 'A') s[cnt].low[i] = s[cnt].str[i] + 32; else s[cnt].low[i] = s[cnt].str[i]; } //将输入的单词字母排序,排序后单词存在low中,原单词在str中 sort(s[cnt].low, s[cnt].low + strlen(s[cnt].str)); //标记为字符串中未出现 s[cnt++].no = true; } //搜索 for (int i = 0; i < cnt; i++) { for (int j = i + 1; j < cnt; j++) { //若存在重排单词 即 单词出现 if (strcmp(s[i].low, s[j].low) == 0) { s[i].no = false; s[j].no = false; } } } int n = 0; for (int i = 0; i < cnt; i++) { //若单词未出现 if (s[i].no) { strcpy(a[n++].str, s[i].str); } } //字典序 sort(a, a + n, cmp); for (int i = 0; i < n; i++) printf("%s\n", a[i].str); return 0; }
相关文章推荐
- UVA 156 Ananagrams (字符串模拟)
- uva 156 Ananagrams(字符串+STL应用)
- UVA 156 Ananagrams 模拟+字符串处理
- UVA 156 Ananagrams 模拟+字符串处理
- UVA 156-Ananagrams(字符串排序按序输出无重复单词)
- UVA 156 【map的使用】【字符串模拟】
- UVA156 Ananagrams(字符串处理)
- UVA ~ 156 ~ Ananagrams(map+字符串处理)
- UVA156 - Ananagrams(同白皮书的例子:字母重排)
- uva 409(字符串模拟)
- UVA 156 Ananagrams
- UVA 1419 - Ugly Windows(搜索,模拟)
- uva 310 L--system(隐式图搜索+字符串处理)
- uva 10274 Fans and Gems(隐式图搜索+模拟)
- uva- 156-Ananagrams
- (STL的使用7.3.1)UVA 156 - Ananagrams(multimap、vector的使用)
- uva156 - Ananagrams
- UVA 156 - Ananagrams
- uva 310 L--system(隐式图搜索+字符串处理)
- UVa156 Ananagrams