Problem:
Problem E:
Full Text Search
Mr. Don is an administrator of a famous quiz website named QMACloneClone. The users there can submit their own questions to the system as well as search for question texts with arbitrary queries. This search system employs bi-gram search method.
The bi-gram search method introduces two phases, namely preprocessing and search:
Preprocessing
Precompute the set of all the substrings of one or two characters long for each question text.
Search
Compute the set for the query string in the same way. Then nd the question texts whose precomputed sets completely contain the set constructed from the query.
Everything looked fine for a while after the feature was released. However, one of the users found an issue: the search results occasionally contained questions that did not include the query string as-is. Those questions are not likely what the users want. So Mr. Don has started to dig into the issue and asked you for help. For each given search query, your task is to find the length of the shortest question text picked up by the bi-gram method but not containing the query text as its substring.
Input
The input consists of multiple datasets. A dataset is given as a search query on each line. The input ends with a line containing only a hash sign ("
#
"), which should not be processed.
A search query consists of no more than 1,000 and non-empty lowercase and/or uppercase letters. The question texts and queries are case-sensitive.
Output
For each search query, print the minimum possible length of a question text causing the issue. If there is no such question text, print "
No Results
" in one line (quotes only to clarify).
Sample Input
a
QMAClone
acmicpc
abcdefgha
abcdefgdhbi
abcbcd
#
Output for the Sample Input
No Results
9
7
9
12
6
Note
Let's consider the situation that one question text is "CloneQMAC". In this situation, the set computed in the preprocessing phase is {"C", "Cl", "l", "lo", "o", "on", "n", "ne", "e", "eQ", "Q", "QM", "M", "MA", "A", "AC"}.
In the testcase 2, our input text (search query) is "QMAClone". Thus the set computed by the program in the search phase is {"Q", "QM", "M", "MA", "A", "AC", "C", "Cl", "l", "lo", "o", "on", "n", "ne", "e"}.
Since the first set contains all the elements in the second set, the question text "CloneQMAC" is picked up by the program when the search query is "QMAClone" although the text "CloneQ-MAC" itself does not contain the question text "QMAClone". In addition, we can prove that there's no such text of the length less than 9, thus, the expected output for this search query is 9.

Submitted solutions:

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(v) (v).begin(), (v).end()
#define resz(v, ...) (v).clear(), (v).resize(__VA_ARGS__)
#define reps(i, m, n) for(int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)

template<class T1, class T2> void chmin(T1 &a, T2 b){if(a>b)a=b;}
template<class T1, class T2> void chmax(T1 &a, T2 b){if(a<b)a=b;}

using Pi = pair<int, int>;
using Tapris = tuple<int, int, int>;
using vint = vector<int>;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

int c2i(char c) {
  return isupper(c) ? c-'A' : c-'a'+26;
}

char i2c(int i) {
  return i < 26 ? i+'A' : i-26+'a';
}

const int n = 52;
int G[n][n];
bool used[1000];
vector<char> path;
void dfs(int u, int& cnt, int e, const int& E) {
  if(e == E) {
    //rep(i, path.size()) cout << path[i] << " ";
    //cout << endl;
    ++cnt;
    return;
  }
  for(int v = 0; v < n; ++v) {
    if(G[u][v] != -1 && !used[G[u][v]]) {
      used[G[u][v]] = true;
      //path.push_back(i2c(v));
      dfs(v, cnt, e+1, E);
      //path.pop_back();
      used[G[u][v]] = false;
      if(cnt >= 2) return;
    }
  }
}

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  string s;
  while(cin >> s, s != "#") {
    int len = s.size();
    if(len <= 2) {
      cout << "No Results" << endl;
      continue;
    }

    fill(G[0], G[n], -1);
    int E = 0;
    vint in(n, 0), out(n, 0);
    rep(i, len-1) {
      int from = c2i(s[i]), to = c2i(s[i+1]);
      if(G[from][to] == -1) {
	G[from][to] = E++;
	++out[from];
	++in[to];
      }
    }
    int D = 0;
    rep(i, n) D += max(0ll, in[i]-out[i]);
    if(D == 0) cout << E+1 << endl;
    else if(D != 1) cout << E+D << endl;
    else if(E+1 < len) cout << E+1 << endl;
    else {
      fill(used, used+E, false);
      int cnt = 0;
      //path.push_back(s[0]);
      dfs(c2i(s[0]), cnt, 0, E);
      //path.pop_back();
      if(cnt == 2) cout << E+1 << endl;
      else cout << E+2 << endl;
    }
  }

  return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 3080
final_rank: 3

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
using namespace std;

inline int a2i(char c){
	if(islower(c))
		return c-'a';
	else
		return c-'A'+26;
}


int main(void){
	string s;
	while(cin >> s){
		if(s=="#") break;

		const int n = s.size();
		if(n <= 2){
			puts("No Results");
			continue;
		}

		int ans = n + 1;
		const int limit = 52;

		bool graph[limit][limit];
		
		rep(i,limit)rep(j,limit) graph[i][j]=false;
		rep(i,n-1) graph[a2i(s[i])][a2i(s[i+1])]=true;

		
		set<int> words;
		rep(i,n) words.insert(a2i(s[i]));

		int in[limit],out[limit];
		rep(i,limit) in[i] = out[i] = 0;
		rep(i,limit)rep(j,limit) in[j]+=graph[i][j],out[i]+=graph[i][j];


		rep(from,limit)rep(to,limit){
			if(words.find(from)==end(words)) continue;
			if(words.find(to)==end(words)) continue;
			
			in[from]++,out[to]++;
			int num = 0;
			rep(i,limit) num += max(in[i],out[i]);
			in[from]--,out[to]--;
			

			bool ok = true;
			if(from == a2i(s[0]) and to == a2i(s[n-1]) and num == n){
				
				ok = false;
				int cur = from;
				rep(idx,n-1){
					const int tar = a2i(s[idx+1]);
					
					bool reach[limit][limit];
					rep(i,limit)rep(j,limit) reach[i][j]=graph[i][j];
					rep(k,limit)rep(i,limit)rep(j,limit) reach[i][j]|=(reach[i][k]&reach[k][j]);

					bool loop = reach[tar][cur];
					if(loop){
						rep(j,limit){
							if(j!=tar and graph[cur][j] and reach[j][cur]){
								ok = true;
							}
						}
					}

					graph[cur][tar] = 0;
					cur = tar;
				}
			}
			if(ok) ans = min(ans,num);
		}
		cout << ans << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 820
memory_kb: 3020
final_rank: 4

code_snippet: #include<stdio.h>
#include<vector>
#include<string.h>
#include<algorithm>
using namespace std;
char str[1100];
int c[1100];
int g[60][60];
int G[60][60];
int in[60];
int out[60];
int UF[60];
int FIND(int a){
	if(UF[a]<0)return a;
	return UF[a]=FIND(UF[a]);
}
void UNION(int a,int b){
	a=FIND(a);b=FIND(b);if(a==b)return;UF[a]+=UF[b];UF[b]=a;
}
int liv[60];
bool chk(int at){
	for(int i=0;i<60;i++)UF[i]=-1;
	for(int i=0;i<60;i++)liv[i]=0;
	for(int i=0;i<60;i++)for(int j=0;j<60;j++){
		if(G[i][j]){
			liv[i]=liv[j]=1;
			UNION(i,j);
		}
	}
	for(int i=0;i<60;i++)for(int j=0;j<60;j++)if(liv[i]&&liv[j]&&FIND(i)!=FIND(j))return false;
	for(int i=0;i<60;i++)if(liv[i]&&FIND(at)!=FIND(i))return false;
	return true;
}
int L[5100];
int R[5100];
int main(){
	while(1){
		scanf("%s",str);
		if(str[0]=='#')return 0;
		int n=strlen(str);
		if(n<=2){
			printf("No Results\n");continue;
		}
		for(int i=0;i<60;i++)for(int j=0;j<60;j++)g[i][j]=0;
		for(int i=0;i<n;i++){
			if('a'<=str[i]&&str[i]<='z')c[i]=str[i]-'a';
			else c[i]=str[i]-'A'+26;
		}
		for(int i=0;i<n-1;i++)g[c[i]][c[i+1]]=1;
		for(int i=0;i<60;i++)in[i]=out[i]=0;
		int m=0;
		for(int i=0;i<60;i++)for(int j=0;j<60;j++){
			if(g[i][j]){
				m++;
				in[j]++;out[i]++;
			}
		}
		int pc=0;
		int mc=0;
		int pt=0;
		int pa=0;
		int ma=0;
		for(int i=0;i<60;i++){
			if(out[i]>in[i]){
				pc++;pt+=out[i]-in[i];
				pa=i;
			}else if(in[i]>out[i]){
				mc++;
				ma=i;
			}
		}
		for(int i=0;i<5100;i++)L[i]=R[i]=0;
	//	for(int i=0;i<60;i++)if(in[i]||out[i])printf("%d %d\n",in[i],out[i]);
	//	printf("%d %d %d %d\n",pc,mc,pt,pa);
		if(pc==0&&mc==0){
			printf("%d\n",m+1);continue;
		}
		int ret=m+pt;
		if(pc>1||mc>1){
			printf("%d\n",ret);continue;
		}
		g[ma][pa]+=pt-1;
		m+=pt-1;
		int at=pa;
		for(int i=0;i<60;i++)for(int j=0;j<60;j++)G[i][j]=g[i][j];
		
		for(int i=0;i<m;i++){
			for(int j=0;j<60;j++){
				if(!G[at][j])continue;
				G[at][j]--;
				if(chk(j)){
					at=j;
					L[i]=j;
					break;
				}else G[at][j]++;
			}
		}
		at=pa;
		for(int i=0;i<60;i++)for(int j=0;j<60;j++)G[i][j]=g[i][j];
		//for(int i=0;i<m;i++)printf("%d %d\n",L[i],R[i]);
		for(int i=0;i<m;i++){
			for(int j=59;j>=0;j--){
				if(!G[at][j])continue;
				G[at][j]--;
				if(chk(j)){
					at=j;
					R[i]=j;
					break;
				}else G[at][j]++;
			}
		}
	//	for(int i=0;i<m;i++)printf("%d %d\n",L[i],R[i]);
		bool ok=false;
		for(int i=0;i<m;i++){
			if(L[i]!=R[i])ok=true;
		}
		if(m+1<n)ok=true;
		if(ok)printf("%d\n",m+1);
		else printf("%d\n",m+2);
	}
}
accuracy: 1
time_ms: 260
memory_kb: 1052
final_rank: 2

code_snippet: #include <iostream>
#include <fstream>
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#pragma comment(linker, "/STACK:400000000")

#define EPS 1e-9
#define INF MOD
#define MOD 1000000007LL
#define fir first
#define foreach(it,X) for(it=X.begin();it!=X.end();it++)
#define iss istringstream
#define ite iterator
#define ll long long
#define mp make_pair
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<n;i++)
#define pi pair<int,int>
#define pb push_back
#define sec second
#define sst stringstream
#define sz size()
#define vc vector
typedef vc<int> vi;
typedef vc<ll> vl;
typedef vc<string> vs;

const int N=128;

string s;
int g[N][N];
int in[N],out[N],E;
int vis[N],twoways;

void dfs(int cur){
	vis[cur]=1;
	rep(i,N)if(!vis[i] && g[cur][i]){
		dfs(i);
	}
}

void go(int cur){
	//cout<<(char)cur<<endl;
	if(E==0)return;
	int successed=0;
	rep(i,N)if(g[cur][i]){
		g[cur][i]=0;
		fill(vis,vis+N,0);
		dfs(i);
		int ok=1;
		//rep(j,N)if(islower(j))cout<<(char)j<<vis[j]<<" ";cout<<endl;
		rep(j,N){
			if(in[j]>0 && !vis[j]){
				//cout<<(char)j<<" "<<in[j]<<endl;
				ok=0;break;
			}
		}
		//cout<<(char)cur<<"->"<<(char)i<<" ("<<E<<") :"<<(ok?"OK":"NG")<<endl;
		if(ok){
			if(successed){
				//cout<<"Found: "<<(char)successed<<endl;
				twoways=1;
				return;
			}
			successed=i;
			in[i]--;
			E--;
			go(i);
		}
		g[cur][i]=1;
		in[i]++;
		E++;
	}
}

int main(){
	while(cin>>s && s!="#"){
		if(s.sz<=2){
			cout<<"No Results\n";
			continue;
		}
		memset(g,0,sizeof(g));
		fill(in,in+N,0);
		fill(out,out+N,0);
		E=0;
		rep(i,s.sz-1){
			g[s[i]][s[i+1]]=1;
		}
		rep(i,N)rep(j,N){
			if(g[i][j]){
				out[i]++,in[j]++,E++;
			}
		}
		int count=0;
		rep(i,N){
			count+=max(0,out[i]-in[i]);
		}
		if(count==0)cout<<E+1<<endl;
		else if(count>1)cout<<E+1+(count-1)<<endl;
		else if(E<s.sz-1)cout<<E+1<<endl;
		else{
			int start=-1;
			rep(i,N){
				if(out[i]-in[i]==1)start=i;
			}
			twoways=0;
			int e=E;
			go(start);
			//cout<<(twoways?"Two":"One")<<endl;
			cout<<(twoways?e+1:e+1+1)<<endl;
		}
	}
}
accuracy: 1
time_ms: 130
memory_kb: 1304
final_rank: 1