zoukankan      html  css  js  c++  java
  • Codeforces 667C Reberland Linguistics 记忆化搜索

    链接

    Codeforces 667C Reberland Linguistics

    题意

    给你一个字符串,除去前5个字符串后,使剩下的串有长度为2或3的词根组成,相邻的词根不能重复。找到所有的词根

    思路

    去掉前5个字符,将剩下的串反过来进行记忆化,用vis[last][pos]记录一下当前状态是否做过。last是之前与之相邻的词根。比赛的时候只用了vis[i]错了。

    代码

    #include <iostream>
    #include <cstdio>
    #include <vector>
    #include <stack>
    #include <queue>
    #include <algorithm>
    #include <map>
    #include <set>
    #include <cmath>
    #include <cstring>
    #include <string>
    
    #define LL long long
    #define INF 0x3f3f3f3f
    #define eps 1e-8
    #define MAXN 10005
    using namespace std;
    
    string s, ss;
    int len;
    vector<string> res;
    map<string, bool> mp;
    map<pair<string, int>, bool> vis;
    int f[MAXN];
    void dfs(int pos, string last){
    	if (vis[make_pair(last, pos)] == true) return;
    	int cur = -1;
    	if (pos >= len) return;
    	for (int i = 0; i < 2; ++i){
    		if (pos + 2  + i<= len){
    			string temp = s.substr(pos, 2 + i);
    			if (!mp[temp]){
    				res.push_back(temp);
    				mp[temp] = true;
    			}
    			if (temp != last){
    				dfs(pos + 2 + i, temp);
    			}
    		}
    	}
    	vis[make_pair(last,pos)] = true;
    }
    
    int main(){
    #ifndef ONLINE_JUDGE
    	freopen("in.txt", "r", stdin);
    	//freopen("out.txt", "w", stdout);
    #endif // ONLINE_JUDGE
    	cin >> ss;
    	ss = ss.substr(5);
    	len = ss.length();
    	for (int i = len - 1; i >= 0; --i){
    		s.push_back(ss[i]);
    	}
    	dfs(0, "");
    	int ans = res.size();
    	printf("%d
    ", ans);
    	for (int i = 0; i < res.size(); ++i){
    		int len = res[i].length();
    		for (int j = 0; j < (len >> 1); ++j){
    			swap(res[i][j], res[i][len - j - 1]);
    		}
    	}
    	sort(res.begin(), res.end());
    	for (int i = 0; i < res.size(); ++i){
    		cout << res[i] << endl;
    	}
    }
    
  • 相关阅读:
    edgecore
    十问 Linux 虚拟内存管理 (glibc)
    Covered Path
    Journey Planning
    K for the Price of One
    Candies!
    2种方式解决nginx负载下的Web API站点里swagger无法使用
    分布式环境下的数据一致性问题的方案讨论
    static,你还敢用吗?
    分离EF connectionString里的db连接串
  • 原文地址:https://www.cnblogs.com/macinchang/p/5448286.html
Copyright © 2011-2022 走看看