C++の同義文
同等の単語の同義語と文のテキストのペアのリストがあるとすると、辞書式順序で並べ替えられた可能性のあるすべての同義語を見つける必要があります。
したがって、入力が同義語=[["happy"、 "joy"]、["sad"、 "sorrow"]、["joy"、 "cheerful"]]のようであり、text="今日は幸せですが昨日は悲しかった」、「今日は元気ですが昨日は悲しかった」、「今日は元気ですが昨日は悲しかった」、「今日は幸せですが昨日は悲しかった」、「今日は幸せですが昨日は悲しみでした」、「今日は喜びですが、昨日は悲しかったです」、「今日は喜びですが、昨日は悲しみでした」]
これを解決するには、次の手順に従います-
-
マップの親、色、およびgroupByColorを定義する
-
関数find()を定義します。これにはsがかかります
-
parent [s]がsと同じ場合、-
-
parent [s]:=find(parent [s])
-
-
親を返す
-
関数unionNode()を定義します。これには、a、b、
が必要です。 -
x:=find(a)、y:=find(b)
-
xがyと同じ場合、-
-
parent [x]:=y
-
-
配列を定義します
-
関数getString()を定義します。これにはtがかかります
-
アレイの温度を定義する
-
終了:=0
-
curr:=空白の文字列
-
end
-
t [end]が空のスペースと同じである場合、-
-
温度の最後にcurrを挿入します
-
curr:=空白の文字列
-
次の部分を無視し、次の反復にスキップします
-
-
curr:=curr concatenate t [end]
-
-
温度の最後にcurrを挿入します
-
温度を返す
-
関数dfs()を定義します。これは文字列、idxを取り、空白の文字列で一時的に初期化します。
-
idxが文字列のサイズと同じである場合、-
-
ansの最後にtempを挿入します
-
戻る
-
-
現在:=文字列[idx]
-
電流がカラーでない場合、-
-
dfs(strings、idx + 1、temp + current concatenate((idx + 1が文字列のサイズと同じ場合は空白の文字列、それ以外の場合は空白スペース)))
-
-
それ以外の場合
-
1つのセットを定義しますx=groupByColor [color [current]]
-
xの各要素zについて、次のようにします-
-
dfs(strings、idx + 1、temp + z +((idx + 1が文字列のサイズと同じ場合は空白の文字列、それ以外の場合は空白スペース)))
-
(zを1増やします)
-
-
-
関数seeGroups()
を定義します -
groupByColorの各要素iに対して、次のようにします-
-
x:=セットとしてのiの2番目
-
1つのセットを定義する
-
xの各要素zについて−
-
(iを1増やします)
-
-
-
関数generateSentences()を定義します。これには、1つの2D配列s、別の文字列tが必要です。
-
n:=sのサイズ
-
初期化i:=0の場合、i
-
x:=s [i、0]
-
y:=s [i、1]
-
xが親にない場合、-
-
yが親にない場合、-
-
unionNode(x、y)
-
-
-
-
c:=1
-
初期化i:=0の場合、i
-
x:=s [i、0]
-
z:=s [i、1]
-
y:=find(x)
-
yがカラーでない場合、-
-
color [y]:=c
-
(cを1増やします)
-
-
color [x]:=color [y]
-
color [z]:=color [y]
-
color [x]がgroupByColorにない場合、-
-
1セットssを定義する
-
xをssに挿入
-
yをssに挿入します
-
groupByColor [color [x]]:=ss
-
-
それ以外の場合
-
xをgroupByColor[color[x]]
に挿入します -
zをgroupByColor[color[x]]
に挿入します
-
-
-
配列文字列を定義する=getString(t)
-
dfs(strings、0)
-
配列を並べ替えます
-
ansを返す
例
理解を深めるために、次の実装を見てみましょう-
#include <bits/stdc++.h>
using namespace std;
void print_vector(vector<auto< v){
cout << "[";
for(int i = 0; i<v.size(); i++){
cout << v[i] << ", ";
}
cout << "]"<<endl;
}
class Solution {
public:
map <string, string> parent;
map <string, int> color;
map <int, set<string<> groupByColor;
string find(string s){
if(parent[s] == s)return s;
parent[s] = find(parent[s]);
return parent[s];
}
void unionNode(string a, string b){
string x = find(a);
string y = find(b);
if(x == y)return;
parent[x] = y;
}
vector <string< ans;
vector <string< getString(string t){
vector <string< temp;
int end = 0;
string curr = "";
for(;end < t.size(); end++){
if(t[end] == ' '){
temp.push_back(curr);
curr = "";
continue;
}
curr += t[end];
}
temp.push_back(curr);
return temp;
}
void dfs(vector <string< &strings, int idx, string temp = ""){
if(idx == strings.size()){
ans.push_back(temp);
return;
}
string current = strings[idx];
if(color.find(current) == color.end()){
dfs(strings, idx + 1, temp + current + (idx+1 == strings.size()?"":" "));
}
else{
set <string< x = groupByColor[color[current]];
set <string< :: iterator z = x.begin();
while(z != x.end()){
dfs(strings, idx + 1, temp + *z + (idx+1 == strings.size()?"":" "));
z++;
}
}
}
void seeGroups(){
map <int, set <string< > :: iterator i = groupByColor.begin();
while(i != groupByColor.end()){
set <string< x = i->second;
set <string< :: iterator z = x.begin();
while(z != x.end()){
z++;
}
cout << endl;
i++;
}
}
vector<string< generateSentences(vector<vector<string<>& s, string t) {
int n = s.size();
for(int i = 0; i < n; i++){
string x = s[i][0];
string y = s[i][1];
if(parent.find(x) == parent.end())parent[x] = x;
if(parent.find(y) == parent.end())parent[y] = y;
unionNode(x,y);
}
int c = 1;
for(int i = 0; i < n; i++){
string x = s[i][0];
string z = s[i][1];
string y = find(x);
if(color.find(y) == color.end()){
color[y] = c;
c++;
}
color[x] = color[y];
color[z] = color[y];
if(groupByColor.find(color[x]) == groupByColor.end()){
set <string< ss;
ss.insert(x);
ss.insert(y);
groupByColor[color[x]] = ss;
}
else{
groupByColor[color[x]].insert(x);
groupByColor[color[x]].insert(z);
}
}
vector <string< strings = getString(t);
dfs(strings, 0);
sort(ans.begin(), ans.end());
return ans;
}
};
main(){
Solution ob;
vector<vector<string<> v = {{"happy","joy"},{"sad","sorrow"},{"joy","cheerful"}};
print_vector(ob.generateSentences(v, "I am happy today but was sad yesterday"));
} 入力
[["happy","joy"],["sad","sorrow"],["joy","cheerful"]] "I am happy today but was sad yesterday"
出力
[I am cheerful today but was sad yesterday, I am cheerful today but was sorrow yesterday, I am happy today but was sad yesterday, I am happy today but was sorrow yesterday, I am joy today but was sad yesterday, I am joy today but was sorrow yesterday, ]
-
C++の識別子
C ++識別子は、変数、関数、クラス、モジュール、またはその他のユーザー定義アイテムを識別するために使用される名前です。識別子は、文字AからZまたはaからzまたはアンダースコア(_)で始まり、その後に0個以上の文字、アンダースコア、および数字(0から9)が続きます。 C ++では、識別子内に@、$、%などの句読文字を使用できません。 C ++は、大文字と小文字を区別するプログラミング言語です。したがって、Manpowerとmanpowerは、C++では2つの異なる識別子です。 受け入れ可能な識別子の例を次に示します- mohd Piyush abc move_na
-
LinuxでのC++の最高のIDEは何ですか?
大きなプロジェクトは、単なるテキストエディタでは管理が困難です。このような場合にIDEを使用すると、生産性が向上し、フラストレーションが軽減される可能性があります。 IDEにはさまざまな種類があり、ニーズに合ったものを選択する必要があります。 Linux上のC++に最適なIDEは1つではありません。ツールは賢く選ぶ必要があります。 Linux用の人気のあるIMOの最高のIDEのリストは次のとおりです。 C /C++開発用のNetbeans- Netbeansは、C /C++および他の多くのプログラミング言語向けの無料のオープンソースで人気のあるクロスプラットフォームIDEです。コミュニテ