字符串训练 ----- UVA 644题目 Immediate Decodability

来源:互联网 发布:现在开淘宝店还挣钱吗 编辑:程序博客网 时间:2024/06/05 06:08


解题思路:  这题主要是判断有没有字符串是另一个字符串的前缀 。。直接用string 的find解题然后判断下返回的位置就ok了。


AC代码如下

#include <iostream>#include <string>using namespace std;int main(int argc, char** argv) {string str[10];int i =0;bool flag = true;int  case1 =1;while (getline(cin, str[i])) {if (str[i] == "9") {// 统计结果for (int k=0; k< i; ++k) {if (flag == false)break;for (int m =0; m< i; ++m) {if (str[m].length() >= str[k].length())continue;if (str[k].find(str[m]) == 0) {flag =false;break;}}} if (flag == true) cout<< "Set "<< case1<<" is immediately decodable"<< endl;elsecout<< "Set "<< case1<<" is not immediately decodable"<< endl;case1 ++;i = 0;flag = true;continue;}++i;}return 0;}


0 0