poj 2408 Anagram Groups 字符串处理题
来源:互联网 发布:webassembly与java 编辑:程序博客网 时间:2024/05/29 03:52
可以练练代码能力
写了个字典树映射,字典树节点中还用一个vector保存了拥有相同构成的字符串,输出时排序一下输出就好了
细节处理需要细心
View Code
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
char str[30010][20];
class trie{
public :
int num,id;
vector<string> ss;
trie* child[27];
trie(){
ss.clear();
num=0;
memset(child,0,sizeof(child));
}
}root;
void insert(int len,char *s,string info,int id){
class trie *cur=&root;
for(int i=0;i<len;i++){
int id=s[i]-'a';
if(!cur->child[id])
cur->child[id]=new trie;
cur=cur->child[id];
}
cur->num++;
cur->ss.push_back(info);
}
int find(char *s){
class trie *cur=&root;
int len=strlen(s);
for(int i=0;i<len;i++){
int id=s[i]-'a';
if(!cur->child[id]) return 0;
cur=cur->child[id];
}
return cur->num;
}
struct node{
int id;
char s[20];
}tmp[30010];
int cmp(node s1,node s2){
return strcmp(s1.s,s2.s)<0;
}
int n,num[30010];
void print(vector<string> V)
{
vector<string> ::iterator it;
sort(V.begin(),V.end());
int tot=unique(V.begin(),V.end())-V.begin();
for(it=V.begin();it!=V.begin()+tot;it++) cout<<*it<<" ";
}
void make(char *s){
class trie *cur=&root;
int len=strlen(s);
for(int i=0;i<len;i++){
int id=s[i]-'a';
if(!cur->child[id]) return ;
cur=cur->child[id];
}
print(cur->ss);
}
vector<int> pos[30010];
void solve(){
map<string,int> mm;
int i,j;
int mx=0,id=-1;
sort(tmp,tmp+n,cmp);
memset(num,0,sizeof(num));
for(i=0;i<=n;i++)pos[i].clear();
for(i=0;i<n;i++){
if(mm.find((string)str[tmp[i].id])!=mm.end()) continue;
int t=find(str[tmp[i].id]);mm[(string)str[tmp[i].id]]=1;
num[t]++;pos[t].push_back(i);
}
int cnt=0;
for(i=n;i>=1;i--){
if(num[i]){
for(j=0;j<pos[i].size();j++){
cnt++;
printf("Group of size %d: ",i);
make(str[tmp[pos[i][j]].id]);
puts(".");
if(cnt==5) return ;
}
}
}
}
int main(){
n=0;
while(gets(str[n])){
tmp[n].id=n;
int len=strlen(str[n]);
strcpy(tmp[n].s,str[n]);
sort(str[n],str[n]+len);
insert(len,str[n],(string)tmp[n].s,n);
n++;
}
solve();
return 0;
}
- poj 2408 Anagram Groups 字符串处理题
- POJ 2408:Anagram Groups
- poj 2408 Anagram Groups(hash)
- POJ 2408 Anagram Groups 笔记
- 字符串依据元素分组 Trie+hash POJ 2408 Anagram Groups
- ZOJ 1960 Anagram Groups(字符串处理)
- POJ 2408 Anagram Groups 字母排序
- POJ 2408 Anagram Groups 排序到极致
- poj----Anagram Groups
- poj——2408——Anagram Groups
- POJ 2408 Anagram Groups(字典树hash)
- POJ-2408 Anagram Groups-字典序取最大前五
- poj-Anagram
- POJ 1256 Anagram
- POJ 1256 Anagram
- poj 1256 Anagram
- poj 1256 Anagram
- POJ 1256 Anagram
- poj 旋转卡壳三题 poj 2187 poj 2079
- hdu 1154 poj 2462 Cutting a polygon 计算几何
- 计算几何模板 更新中
- 半平面交入门 poj 3335 poj 1474 poj 1279 半平面交模板
- poj 1514 Metal Cutting 半平面交的平面切割
- poj 2408 Anagram Groups 字符串处理题
- 半平面交 求解最大内接圆
- poj 1383 广搜 类似于树的直径
- hdu 3911 线段树基本功 区间合并
- 线段树练习 区间合并
- poj 2385 动态规划
- ZSTU 3719 AC自动机DP
- poj 3336 计算几何 + bfs
- 状态压缩DP 求简单图的环的个数